@cdjb's answer is better. This is supplementary. Read that first.
What cdjb describes has happened in the actual presidential contest. It's unlikely, but can happen. In 1824 Jackson entered with the most popular and electoral votes, but Adam's was elected presedent.
(Electoral College runs on the same principles, but is governed by different rules that basically do the same.)
Broadly, if you have delegates voting for the winner, require an outright majority to win, and have 3+ candidates, it's common to proceed to multi-round voting.
It's seen as important to have an outright majority to win so nobody can win by splitting their opponents. If Literally Hitler got 20% of the vote, but the rest were evenly split across 5 candidates, going to further rounds of voting might show 80% would prefer Bob over Hitler. These further rounds ensure the victor has more than minority support.
Subsequent rounds may have additional rules, often to cut down on the number of options to help consolidate the vote and get it closer to 50%.
(This is very related to runoff voting.)