I'm going to assume that you're somewhat familiar with the structure of a .WAV file : it contains a WAVEFORMATEX header struct, followed by a number of other structs (or "chunks") containing various kinds of information. See Wikipedia for more info on the file format.
First, iterate through the .wav file and add up the the unpadded lengths of the "data" chunks (the "data" chunk contains the audio data for the file; usually there is only one of these, but it's possible that there could be more than one). You now have the total size, in bytes, of the audio data.
Next, get the "average bytes per second" member of the WAVEFORMATEX header struct of the file.
Finally, divide the total size of the audio data by the average bytes per second - this will give you the duration of the file, in seconds.
This works reasonably well for uncompressed and compressed files.