It's a combination of terrain generated randomly by code, and a lot of typically much smaller hand-crafted maps using a special map syntax (these are called "vaults" and are placed randomly within the bigger code-generated layouts).
This is a very simple explanation but it's mostly the case, there are some exceptions.
A lot of the layout code is in crawl/dat/des/builder/layout*.des, and a little bit in crawl/dat/dlua/*.lua. All the hand-crafted vaults are in *.des files in other subfolders of dat/des. As you might guess from this, a lot of the layout code is written in Lua, although some of the layouts call C++ functions in the Crawl source code.
If you want to understand the vault syntax better you can start with the online reference:
https://crawl.develz.org/wiki/doku.php?id=dcss:help:mapsIt's very easy to start making these maps and new contributions are always encouraged (although read the guidelines first!); the more vaults get added the more interesting, diverse and surprising the levels can become.