The thing to understand here is that the Constitutional Convention of 1787, which is where all this got decided, was full of compromises. The idea when answering questions like "how should we elect the president? how long should he serve? should we even have one single president, or a committee?" wasn't to arrive at the objectively best solution, but something that as many people were okay with as possible. The structure of our government isn't the Platonic ideal -- it's what worked for most of us when it was passed.
The other thing to understand is how awful communication was back then. Most of the delegates were leery of having the general public decide big elections not because "lol John Q. doesn't know jack, stupid proles" but because the local newspapers that informed most voters tended to be sacks of crap. They didn't cover current events, and they couldn't be trusted to help people make an informed decision. Voting for members of the state legislature and House of Representatives was small enough that people could probably get a decent idea of what was going on.
The other major concern for a direct popular vote was regionalism: if a significant fraction of the people simply voted for a candidate from their state, national unity would be out the window. Yes, I know, I've banged this drum before, but it was a serious fear back then. Calling the former British colonies the United States of America didn't magically make everyone join together, and there were already tensions between the states over commerce, industry, and of course slavery.
Frankly, there were a few proposals for electing the president. Some wanted the governors to pick him, and Madison favored Congress having the responsibility. Nobody liked the idea too much, but having states send as many people as they had votes in Congress to decide who should be president seemed like the least bad option available.
More than a few people thought there was a decent chance that the Electoral College wouldn't have a majority for a single candidate; Madison was betting that this would happen nearly every time. There was a lot of contention over how the election would proceed in that event, since the general preference was for the House, which was closest to the people, to choose, but that kinda screwed over the small states. That concern got assuaged by giving each state delegation one vote, just like in the Senate, where each state got two votes; that arrangement was echoed in the Senate's role in the election process, since they chose the vice president, each senator getting one vote.
The Electoral College was a complex system, set up to try to keep as many people semi-satisfied as they could manage. It still practically fell on its face the first time there was an actual race, and had even bigger problems the next time. More on that tomorrow.