How does dwarf fortress generate the civilizations while doing world gen? Does it just pick places (that are not ocean) at random, or is there a more advanced algorithm?
You can get some idea of what happens by opening your generated world in legends mode. There's an option that lets you see the spread of different civs over time on a map.
Basically, the way it seems to work is that the game randomly picks suitable initial city sites for each civ, and they gradually spread out from there, founding new sites. Where the areas of influence of two civs overlap, they may come into conflict. Apparently, this is tied to the history generation, which records things like the founding and fall of city sites, as well as major battles (which might sometimes result in a site changing ownership).
Also, different civs appear to have different requirements for starting sites: for example, elves always start in a forest, while dwarves seem to start near mountains and humans on plains. Evilness, savagery and aridity also seem to affect things; dial those high enough in the worldgen params, and it's quite easy to end up with a world with no playable civs.