I thought something like the ROM/RAM mixing in cartridges must be the case. But then how on earth do people make files of those games that can then run on emulators?
People have assembled lists of all known cartridge configurations (as many games tended to share the same overall configuration), and assigned them IDs. The ROM specifies, in its header, the ID of the cartridge configuration it needs. (In NES jargon these are called "mappers" [1].) The emulator is expected to hard-code support for every single mapper; this is, of course, a huge nuisance and source of complexity, but there's little that can be done about it.