Live public transport routing

We've all used a public transit router to find a quick route from A to B, such as Google Maps or CityMapper, but how do they actually work?