It detects engine 'knocking' or 'pinking'. That is, an uncontrolled and mistimed deflagration of the fuel-air mixture. (The term 'detonation' is technically incorrect, but it's the one used.)
Generally I think they are acoustic sensors with some signal processing to detect the different sounds (the 'knocking' etc refer to the different sound produced). If the sound sample is timed properly with the ignition sequence the method is fairly accurate.