Gravity and physics.
The train just sits on the tracks, but the wheels of the train are designed a bit like cones (shallow part cones, but cones nevertheless) so they always stay within the confines of the rails, even on turns. In a nutshell, the inside wheel sinks a little deeper while the outside wheel rises a little.
This is a very fine balance. It’s perfectly possible for the train to derail if it takes a turn too fast, so all curves on railways have posted speed limits. Trains on very curvy tracks, like mountain railways in Austria, also use wheels that can pivot so that the wheels stay more flat. However, on conventional trains, the wheels are fixed in a plane perpendicular to the direction of travel so, unlike an automobile differential, they’re always trying to move straight. As such, on turns, this cone shape also allows the wheel on the outside to move faster relative to the curve without actually requiring the axle to twist.