The knock sensor is the unit in the car that protects against incorrect timing issues. It allows the engine to run with the timing advanced as far as possible without damaging the motor or loss of power. Unlike oxygen sensors or the air fuel ratio sensor the knock sensor will not allow the car to run incorrectly. A knock sensor feed info to the ECU and if the car is knocking too much to be adjusted by timing to will send the ECU into a safe mode which will stop the engine all together.
The knock sensor works by responding the knock caused by pre-detonation of the air/fuel mixture. When the fuel mixture in a cylinder is ignited there is a flame front that moves out from the source of the spark. When this pressure wave hits the cylinder walls it makes a noise and this is detected by the Piezoelectric element in the sensor and communicated to the ECU.
When the ECU detects these that are not at the correct time it triggers an adjustment in the motors timing. If the knock sensor fails it can cause the vehicle to run very rough or not start at all. When failure occurs a trouble code is triggered which may show as the sensor or as something else in the motor. A huge factor to remember with ECU codes is that they only show the source of the code, not the issue. In other words the code may be caused by a loose wire but it says knock sensor, it is important to remember this when diagnosing an issue.