Americans do not actually vote for president. They vote for a slate of electors, and five times in history those electors have crowned the candidate who lost the popular vote.
When you vote for president, you are technically voting for a group of electors pledged to that candidate. These electors, 538 in all, are the Electoral College, and a majority of them, 270, is what it takes to win the White House.
Each state gets electors equal to its number of House members plus its two senators. The framers designed this in 1787 as a compromise. Some wanted Congress to pick the president, some wanted a direct popular vote, and the College threaded between them, filtering the public's choice through the states.
Almost every state awards all its electors to whoever wins that state, winner take all. That is why campaigns pour everything into a handful of swing states and largely ignore the rest. Your vote's weight depends heavily on where you cast it.
And because the College counts states, not raw national votes, the winner of the most votes nationwide can still lose. It has happened five times, including in 1824, 1876, 1888, 2000, and 2016.