The following is written after a day spent looking at Map.h - it shouldn't be considered as definitive.
Squares of terrain are defined by three basic properties - the undertile, the main tile, and the main tile's subtype.
The undertile is the primary property, which the other two values are based on. The undertile is set manually (e.g. in the editor) to either water, sand, or grass. Since water and grass aren't allowed next to each other, sand is inserted in-between.
The main tile is generated from the undertile, through a somewhat mysterious process in Map::lookup(). The map tile used for a given square is based on the undertile of the current square, and the undertiles of the squares immediately below, right, and below-right of it. I suspect this process might be to do with handling edges correctly - for example, grass at the edge of an ocean (and at the corner of a pool) appears to be a special type of main tile. However, that is just speculation - I'm not even sure that the main tile will be of the same type as the undertile (e.g. the undertile could be water, the main tile sand).
The main tile's subtype is a random number between 0 and 7, or sometimes 0 and 15. It doesn't convey any meaning, it just controls which exact image is displayed for the tile.