Keep right is a maritime thing and goes back to when ships had steering oars. Because most people are right handed the steering oar was placed on the right hand side of the ship to be operated by the helmsman's right hand. To prevent the steering oars of two ships colliding when passing, ships passed on the right, and this tradition remained when steering oars were replaced with stern mounted rudders. The same situation also lead to Port (originally Larboard) and Starboard. It also lead to another important rule carried through onto the roads and railways, and now universal in so many ways, that of Red for Stop and Green for Go.
Keep left is also down to right handedness, but had land based origins. Right handed people used their sword with their right hand, so it was easier to fight an opponant to the right than the left. Since in those dark and dangerous times, everyone you passed on the road was a potential opponant (getting that way again), it was best to pass people on the left. Also since people's swords were carried on the left hand side, passing to the left prevented swords catching each other.
Keep right on land though also has a land based origin. When working with a team of horses, riders would have their whip in their dominant right hand, and would therefore sit to the left. As is still true today, with road vehicles it is best to pass nearside to nearside, as it gives a better view when passing a vehicle, horse or pedestrian.
Whilst nearside to nearside passing is universal on the roads (other than for vehicles designed for countries where they drive on the opposite side), on the railways offside to offside is more common. That is because signals are commonly placed on the outside of a double track formation, and having the driver on the same side as the signals gives a better view. Unlike on the roads, train drivers have no need to observe passing trains as there is no chance of a collision (and if there is there is nothing they can do). It also has the later benefit of making single manning and DOO easier as the driver is mostly on the platform side. The GWR though chose to have drivers on the inside. Other than in parts where there is interworking between left hand and right hand running trains, or short sections where the running is swapped (such as sections on the Central and Victoria lines), having drivers on the inside is rare.