Let's face it Clinton had this election won until a few things happened the last week before November 3rd. It's didn't help that Comey revisited the email scandal. And it didn't help that the polls were quite favourable for Clinton. And it's clear Clinton misread the " movement " Trump created. 
Trump didn't expect to win , but his supporters did. That showed how desperate they were. They turned out and voted strategically in the blue states. Finally, the polls misread the built up antagonism towards Clinton. 
Her husband's legacy certainly contributed.