When the United States was founded, there was a great debate between the representatives of the small states and the big states concerning representation. The result was the "Great Consensus" and the bicameral Legislature with the House representation based on state population and the Senate which has equal representation.
Part of the consensus between the states was to have our president elected by an electoral college and not purely by popular vote. Each state is given delagates according to the total number of House and Senate members. Accordingly, as happened with Trump and Clinton election, Hillary has narrowly won the popular vote but Trump has won the majority of state electoral votes.
The purpose of having our elections based on electoral has an important effect on elections. If elections were based purely on the popular vote, candidates would spend most of their time in the most populated cities such as New York, Chicago, Los Angeles, and Houston. Small states would never see candidates. However, because the the electoral college, we have the phenomenon of swing and battle-ground states, where even a medium or even smaller state like South Carolina can make all the difference in an election.