Fanworks are a legally gray area because some use of other people's works without permission is allowed. This includes using pieces for reviews and commentary, and transformative uses of the original work. The trouble is figuring out what's "transformative."
"Derivative" works are a copyright violation (in the US) IF the copyright owner complains. But if they don't care, it's fine, and they can decide what's worth their effort; it's not like trademark where they have to do something about infringing works. They can decide "that fangame is derivative, but I don't mind it; I'm not going to have it removed" and they can still go after other copyright violations.
"Transformative" works, including "parody" (which is a legal term; doesn't mean it has to be funny), are legal uses of someone else's material, even without permission, even if they hate it.
The line between "derivative" and "transformative" is decided in court, for individual cases, and is inconsistent as hell. People write dissertations on this. People get PhDs trying to describe the differences.
But the key part is: Only the rights-owner can complain about copyright infringement. If it's not your IP, it's not your problem if someone else made a game from it.
If you want to make fan games, your best way to make sure you're in the clear, is knowing that the IP holder doesn't mind them. (Actually best is formal written permission, but nobody is expecting that. But knowing that the original rights holder is cool with fan games is pretty close.)
Japan has a much different culture about derivative/transformative uses of copyrighted material, which is why you see a lot of anime/manga zines and doujinshi and fangames.
(IANAL; TINLA)