Can measuring the time between seeing the lightning and hearing the thunder really tell you how far away the lighting struck? If so, then how does it work?

the speed of the flash (light) is 3x10^8 m/s (practically instantaneous)

the sound (thunder) travels about 343 m/s in air

so for every 3 seconds between the flash and the boom
... the lightning is a kilometer distant