You can think of it like this:
1) when something is completely enclosed by something much larger -> "in"
2) when something is enclosed partially, or in certain dimension only -> "on"
You are "in" a house since a house encloses you in every direction.
The beach is much larger than you horizontally, but not vertically as you stand on it. So "on".
You are "in" a city, in a province, in a country, and in the world, as they are all big and surrounds you in pretty much every direction.
But you are on a river, on the ocean, and on the ground since they have clearly defined surface on which you stand or float.
Also "in" and "on" is not always fixed.
Wile you can be on the ground, you can be "in" the ground too.
For example if someone dies and is buried, they are said to be "in" the ground.
So instead of just memorizing set phrases, one should acquire the sense of it.