You already got answers about the difference between game mechanics and artwork, so I only add one more sentence that might help describe the difference:
Ideas cannot be copyrighted, but the implementation of ideas with specific names and sequences can.
So you can make something similiar to another as long as you don't use any of the names, artwork, musics etc. created for the other implementation.
But to your other question:
Also, how about fan game?
Fangames are always and automatically illegal, because they use the name of the game they're a fan of.
But copyright and trademark laws are civil laws, not criminal laws, and that means that ONLY the copyright holder can sue you, no one else. And the IP holder needs to know of your work before they can do anything about it.
That is why sometimes fangames seem to be allowed because they existed for years before they suddenly get taken down - like for example the Pokemon Essentials package for RMXP that always contained stolen assets and always was illegal but existed for a decade before the IP holder learned of it and took it down everywhere.
So basically you can get away with the illegality of a fangame as long as you remain obscure - but get a number of praises and let's play on youtube or a similar common site and your fangame will be gone faster than you can look.