Here's why:
* Harry Potter and the Order of the Phoenix saw Gryffindor gain a significant number of points due to the events of the Triwizard Tournament (Harry gaining points for saving Cedric) and the fight against the Death Eaters at the Ministry of Magic.
While Gryffindor generally wins the House Cup in most books, it's not always by the largest margin.
Let me know if you'd like to know the point totals for other books!