It is one of the maddening features of the American system that such a thing can happen, and in fact it has happened three times.
A U.S. Presidential candidate can get most of the popular votes and lose the election.
In 1824, Andrew Jackson got 155,872 votes but lost to John Quincy Adams, even though Adams received only 105,321.
In 1876, Samuel Tilden got 4,284,757 votes, but widespread fraud by Republican-controlled electoral commissions in three states got Rutherford Hayes elected with 4,033,950.
Again in 1888, Democrat Grover Cleveland received 5,540,050 votes, yet lost to Benjamin Harrison’s 5,444,337 votes.