Darrell M. West of the Brookings Institution explains the history of the electoral college and why this antique process for choosing the president should be abolished. 

He begins:

The framers of the Constitution set up the Electoral College for a number of different reasons. According to Alexander Hamilton in Federalist Paper Number 68, the body was a compromise at the Constitutional Convention in Philadelphia between large and small states. Many of the latter worried that states such
as Massachusetts, New York, Pennsylvania, and Virginia would dominate the presidency so they devised an institution where each state had Electoral College votes in proportion to the number of its senators and House members. The former advantaged small states since each state had two senators regardless of its size, while the latter aided large states because the number of House members was based on the state’s population.
In addition, there was considerable discussion regarding whether Congress or state legislatures should choose the chief executive. Those wanting a stronger national government tended
to favor Congress, while states’ rights adherents preferred state legislatures. In the end, there was a compromise establishing an independent group chosen by the states with the power to choose the president.
But delegates also had an anti-majoritarian concern in mind. At a time when many people were not well-educated, they wanted a body of wise men (women lacked the franchise) who would deliberate over leading contenders and choose the best man for the presidency. They explicitly rejected a popular vote for president because they did not trust voters to make a wise choice.

In most elections, the Electoral College has operated smoothly. State voters have cast their ballots and the presidential candidate with the most votes in a particular state has received all the Electoral College votes of that state, except for Maine and Nebraska which allocate votes at the congressional district level within their states.

But there have been several contested elections. The 1800 election deadlocked because presidential candidate Thomas Jefferson received the same number of Electoral College votes as
his vice presidential candidate Aaron Burr. At that time, the ballot did not distinguish between Electoral College votes for president and vice president. On the 36th ballot, the House chose Jefferson as the new president. Congress later amended the Constitution to prevent that ballot confusion from happening again.

You will find this to be an interesting account.

There have been a few elections where the popular vote and the electoral vote differed. In recent years, there were two. Al Gore beat George W. Bush by half a million votes while losing the election. Hillary Clinton won nearly three million votes more than Trump, who won the electoral college.

What kind of democracy elects the loser of the popular vote as its leader?