The other night, Joshua Foer posed this question was posed to a table full of science journalists. Most of us started talking about friction, and/or possibly something to do with the little flanges on either side of a train wheel.
We were all wrong.
This is a Richard Feynman video, yes, but it's more about mechanics than physics. Turns out, you can learn a lot about how trains stay on the track by looking under your own car.