This is a classic topic that seems simple at first glance but is actually a lot more complicated: What keeps trains on tracks? Everyone has seen train tracks and train wheels so you know what they look like. The simplest and most obvious answer is that the flanges (the wider parts on the insides of the wheels) keep the trains on tracks. Partly yes, but the flanges are actually just a safety feature that keeps the train from slipping sideways off the track by accident. When you consider a train going 160 km/h (99.4 mph) you can imagine the screeching that the flanges would create if they were in contact sideways with the tracks. So normally the flanges don’t touch the tracks at all.
What really keeps a train on tracks is actually pretty clever: The wheels are not cylindrical but conical. The tops of the rails are a bit convex and the rails are also not strictly vertical, but they are canted the same amount inwards as the wheels are conical. The convex rail causes the wheels to touch the rails only at a single point. The image below hopefully helps to illustrate the geometry:
Because the wheels are fixed to a rigid axle, they rotate at the same pace. When the tracks start to turn (left in this example), the train tries to go straight and the outer (right) wheel’s point of contact with the rail moves inwards. The inner wheel’s point of contact in turn moves outwards. Now because the wheels are conical, the wheels’ effective circumference (circumference at the point where the rail touches the wheel) is no longer the same, but instead the outer wheel travels longer distance each rotation than the inner. This makes the outer side move forward faster than the inside, which turns the train back to where the tracks lead.
Real tracks are also not level with each other in curves, but instead the outer rail is higher than the inner. This helps the train to turn and allows for higher speeds. Train tracks usually have long turn radii too, so the trains don’t need to turn that fast. How about trams then?
Trams usually share the street space with cars, bikers and pedestrians so canted trackbed is usually out of the question. In a city you can’t have turning radii of hundreds of meters either so trams need to be able to make sharper turns. This is why trams cheat a bit.
A train’s wheel will most likely break if the train runs on the flange, but tram wheels are actually built so that they can do that. This helps in sharp turns, when the outer wheel can transition to run on the flange instead of the regular rim, which makes that wheel’s effective circumference a lot longer than the inner wheel’s, thus turning the tram faster.
All of this is of course a simplified version about what’s happening. There’s a lengthy explanation in Wikipedia about Hunting Oscillation (that is still a simplified version instead of a full analysis) which explains what kind of forces act on train wheels and tracks and how they interact. The page is really worth a look since it has a really nice animated image that shows how a wheel set moves around. I have to warn you though that the train tracks rabbit hole in Wikipedia is deeeeeeeep.
And if my explanation wasn’t convincing, here’s someone you’re probably more inclined to believe: