As has been mentioned, not all cameras store the serial number. However, most of the DSLRs do.
The serial number is actually stored in the MakerNotes section of the EXIF. This is a proprietary section for manufacturers to write in whatever format they like.
I've extended the great work by Jacob Seidelin (< edit: the URL is a 404) to extract the serial number with javascript on my Site which includes an offline version for Chrome for no other reason than to see if I could! It's open source so if you're looking for a javascript solution you're welcome to build on it etc.
The question doesn't include a programming language or any contsraints so the best answer currently is to use exiftool. It has the most complete support for different camera models and file formats (including brilliant support for how different applications butcher it!)
A simple example command to get the serial number would be:
exiftool -SerialNumber myImage.jpg
I writing my site, I've looked into this topic over hundreds of hours so if you have any more questions, let me know!