If that's the case what about doujinshi? How do they fall in line with copyrights? And I know I have seen a ton of fan games involving Nintendo characters.
all of that breaks copyright. But copyright is a civil law, not criminal law, and because of that only the copyright holder himself/herself can initiate the lawsuit, and he/she needs to know of an infringement to start that.
As soon as the IP holder knows of something, it usually gets taken down. Best example is the Pokemon essentials for RMXP that have been around for ten to fifteen years before the IP-Holder learned of them and took them down everywhere.
I am unsure of how the fair use works.
The key to allow fair use is that it requires the work you do to have one of a specific and limited number of uses. If your work does not have one of these uses, then you cannot claim "fair use" - and "having fun" is not an allowed purpose.
For a game, there are only two possible purposes that could be claimed: Education or Parody. Everything else of "fair use" cannot fit.
And since it is difficult to explain why someone needs to (for example) learn the history of a fictive world of Pokemon, the only real option for a fangame to claim fair use is a parody.
So if you want to make a parody of a fandom, a game that makes the player laugh at it, then you can claim fair use. But very few fangames do that, because most of those developers don't want their players to laugh at their beloved fandom.
For details, you can better read up fair use in wikipedia
en.wikipedia.org
But you should keep another thing in mind:
The fair use law is a defense, nothing else. Which means to claim it you already have to be sued against it, and then a judge has to decide if you can claim fair use. And if that fails, you've got to pay all those lawyers, which is why you should not plan on using it without understanding it.