I want to look at the election first from what went wrong on the losing side, though ultimately I think the election was WON by Obama and not LOST by McCain (though Republicans are now trying to peddle the "McCain lost it" idea so as to discount the power of Obama.)
The first thing that went wrong for the Republicans in this election was that they chose the wrong candidates.
McCain had a powerful personal story, and a reputation as someone who would buck his own party and thus appeal to Independents, but he was also 72 years old, and it showed. His forgetfulness, his obvious contempt for Obama, and his confused reaction to the economic crisis all showed a man not in his prime, a man too much a prisoner of his own emotions, a man with possible early dementia. No one ever spoke of that latter possibility, but voters could see the imperfections in the man, the fatigue, the mere reflection of the man he once was, and the lack of intellectual sharpness. In this time of multiple crises in America, they didn't trust him to be in charge.
When McCain chose a completely unqualified woman to be his vice presidential running mate, it only magnified the problems with his candidacy. Sure, she energized the "culture wars" base, and more than a few men who saw her as a sex symbol, but Independents and serious Republicans, even those ideologically in tune with McCain, were repulsed by the choice. First, it proved McCain was impulsive and lacked good judgment. Second, it showed McCain far more interested in a gimmick to help him win by a hair (which is all he would ever win by) than in the welfare of the country. That he would risk putting the nation in the hands of a totally unqualified person was too much even for some Republicans to swallow. By the time of the election, 60% of the electorate deemed her unqualified and many of them who once admired McCain turned away from him because of it.
The contrast of the intellectually superior, steady and optimistic, and highly disciplined Obama with the intellectually shallow, emotionally erratic, and often confused (with respect to message) McCain was obvious to most voters and ultimately it hurt McCain even with members of his own Party.
The first thing you have to have in a presidential candidate is someone who is qualified and relevant NOW, not someone who has a good story from forty years ago or someone who might be ready ten years from now. Obama is in his prime now, writing a story for today, addressing the issues of the moment, while McCain's compelling story is from forty years ago, his prime issue - the need to be victorious in war - still tied to the failure in Vietnam.
So those three things - poor judgment, a candidate past his prime and out of step with the times, and intellectual and emotional inferioriority - all made McCain too much of a risk for the voters, especially when he wanted to follow an intellectually incurious and unqualified president of his own Party into the White House.