The US president is chosen via an electoral college system, not a popular vote. Each state has the number of electors roughly proportional to its population. For example, the state of California has 55 electors, and a population of 39.5 million. Wyoming, on the other hand, only has 3 electors, proportional to their population of ~600,000 people.
All states with the exception of Maine and Nebraska have a winner-takes-all rule (ie in California if the Democratic candidate gets 50.1% of the votes, they win 55 electoral votes).
In order for a candidate to win the presidency, they need at least 270 electoral votes of the possible 538 votes.
If neither candidate gets 270 electoral votes, then the House of Representatives votes to elect the president. However, this is very unlikely to happen with two parties controlling the system.