On Linux/Unix based systems you can use the file command, but I assume you want to do this manually yourself in code...
If all you have access to is the byte stream of the file, then you would need to handle each file type independently.
Most programs/components that do what you are wondering usually read the first few bytes and make a classification based on that. For example GIF files start with one of the following: GIF87a or GIF89a
Many file formats have the same signature at the start of the file, or have the same header format. This signature is refered to as a magic number as described by me on this post.
A good place to get started is to go to www.wotsit.org. It contains the file format specifications searchable by file type. You could look at the important file types that you want to handle and see if you can find some identifying factor in those file formats.
You could also search Google to try and find a library that does this classification, or look at the source code of the file command.