I'm not an expert on file formats so I looked into Wikipedia. Here's what it says on PNG[1]:
A PNG file starts with an 8-byte signature.
The hexadecimal byte values are 89 50 4E 47 0D 0A 1A 0A;
the decimal values are 137 80 78 71 13 10 26 10.
So if a file starts with 89 50 4E 47 0D 0A 1A 0A, you know it may be a valid PNG, otherwise you know it's not.GIF starts with another marker at zero offset, so no valid GIF is a valid PNG, and vice versa.
Some formats are mutually exclusive because they “fight” for contents of first several bytes.
Some formats are more relaxed and introduce the exploited possibility of carefully engineered ambiguity.
edit: removed a section that was utterly wrong