This kind of thing doesn't happen very much — but it's exactly why the Founding Fathers mandated the elector system rather than direct democracy. The goal, as Alexander Hamilton put it in the Federalist Papers, was to ensure "that the office of President will never fall to the lot of any man who is not in an eminent degree endowed with the requisite qualifications."