ROMs themselves are illegal, and we won't tell you where you can get them, but an emulator is something that allows hardware to pretend that it is different hardware.
Then, the game file (ROM) thinks that it is being run by the proper machine and actually runs (with errors, occasionally. Nothing is perfect. 😛).