To the best of my knowledge is never stated directly, but it is implied that the students are sorted into roughly equal numbers in the 4 houses at Hogwarts. There are 7 years and each house has about 10 students per year, or 280 students at the school at any one time, assuming constant class sizes and no dropouts.
If all of the students ride the Hogwarts Express and the cars are standard size, then it has 9 compartments per car, each of which can hold at least 4 students. In this case, the train only needs 8 passenger cars to hold everyone comfortably (at 4 passengers per compartment.) In order to space people out to the degree that we see in the books, you'd want 12-15 cars.
When we see the Hogwarts Express in the movies it only has 3-4 passenger cars, each of which has 8 windows, and presumably 8 passenger compartments per car as a result - far too few for comfortable seating unless it is magically larger inside than outside, such that compartments are possible on both sides of the train, unlike a normal compartment car. Even then, the train would be right at the capacity for students. (4 cars x 16 magical compartments per car x 4 students per compartment = 256 students)