Many students used magic on the train. In Goblet of Fire, Hermione used "Reparo" on the train; In Half-Blood Prince, Ginny used "Scourgify" in the train when there was "Stinksap" all over the compartment. Why didn't the Ministry of Magic consider this as underage magic when students are constantly reminded that they cannot use magic outside SCHOOL?
In real life, it is not uncommon for the trip to and from school being part of a school's responsibility (sometimes even covered by a school's insurance).
The Hogwarts Express doesn't sound like a regular service, but more like a charter by Hogwarts, so it can be considered Hogwarts for all intents and purposes.
It's most likely that the Hogwarts Express is considered an extension of Hogwarts itself. This would be the only reason as to why Underage Magic is performed on board with no penalty.
There have also been examples of underage magic being performed aboard the Hogwarts Express without penalty, perhaps suggesting the train is considered an extension of the school grounds (such as when Ginny Weasley used the Scouring Charm to clean up a compartment following a mishap with one of Neville Longbottom's plants.
Order of the Phoenix (Scourging Charm use on the train)
There's quite a few examples of magic being performed on board before reaching the school.
Now according the wikia the Hogwarts Express was specifically used to transport children without suspicion using a concealment charm (which prevents detection from muggles).
Sometime in between 1819 and 1849, Ottaline Gambol rose to the office of Minister for Magic, and she made a daring and controversial suggestion to solve the ages-old problem of how to transport hundreds of students to and from Hogwarts Castle every school year without attracting the Muggles' attention: intrigued by the Muggle technology, the Minister saw the potential of using a train as a secure and comfortable alternative to Portkeys or to unregulated means of travel. The Ministry of Magic conducted a a large-scale operation involving one hundred and sixty-seven Memory Charms, as well as the biggest Concealment Charm ever performed in Britain, in order to acquire the locomotive. The morning after this operation, the residents of Hogsmeade awoke to find the gleaming red Hogwarts Express and a railway station that had not been there previously, and the Muggle railway employees in Crewe had the feeling they had misplaced something, which stayed with them for the rest of the year. There was initial resistance from pure-blood families against using a Muggle-built device for wizard transportation (which, they claimed, was "unsafe, insanitary and demeaning"), until the Ministry decreed that students would arrive to school on the train or not attend at all.
This overall allows the safe transportation of students to Hogwarts, and I also believe it allows them to cast magic without penalty due to the extremely large Concealment Charm as well (muggles wouldn't be able to witness any acts of magic and students usually refrain from performing dangerous spells on board as well for the most part).
The trace is used to determine the usage of magic anywhere near a minor, not just magic from the minor specifically (which is also why Harry was blamed for Dobby using magic in book 2).
In case of Muggle-born children, it is quite easy to detect, since they are the only ones who can cast magic. In case of wizard/witch-born children, the ministry assumes that the parent will keep their children in check.
In case of the train, there are usually teachers or other witches/wizards on there.
It is explicitly stated in the last chapter of Chamber of Secrets that magic is allowed on the train, and the ban does not take effect until the holidays begin (my emphasis):
Too soon, it was time for the journey home on the Hogwarts Express. Harry, Ron, Hermione, Fred, George, and Ginny got a compartment to themselves. They made the most of the last few hours in which they were allowed to do magic before the holidays. They played Exploding Snap, set off the very last of Fred and George's Filibuster fireworks, and practiced disarming each other by magic. Harry was getting very good at it.