It is not legal have a copy of a ROM file that you do not own a physical copy of - this is sort of like the legality of music downloading.There are a few important points to consider before ROM hacking though.
I'm not an expert in any sense of the word, but as I understand it, yes, ROM hacking as we do it on this forum is legal as it falls under fair use.