You can use `file` command:
$ file file.png
file.png: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced
$ mv file.png file.txt
$ file file.txt
file.txt: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced
The `file` does some tests on file to determine its type. Probably the most important test is comparing a magic number (string in a file header) with pre-defined list.