Actually I don't think the emulators themselves are illegal, or else Nintendo and other console companies would've already shut them down. What is illegal is the use of the emulator to play commercial games (and their derivatives) without having paid for them. An emulator is just software that recreates a console in a different hardware and doesn't have to be used to play commercial games. It can also play indie games and software (called homebrew), which is perfectly legal.
Being in possession of a ROM (read-only memory) copy of a game is only illegal if you don't own the game, because it can be used as a backup copy. Of course, this site and many others forbid ROM distribution because you can't tell whether the person who downloads the ROM actually has the game or not, therefore distributing ROMs is illegal. However, you don't need to download a ROM from the internet - you can copy it from your legit cart using homebrew software and certain hardware, which creates a digital copy of the game for you to edit to your heart's content.
It's so easy to find ROMs on the internet that it's very unlikely you'll ever get in trouble for downloading them.