There was a superstition that if you say you wish that something would happen, evil spirits will make sure that exactly the opposite will happen. Knocking on wood supposedly prevented this. Instead on actual knocking, people started saying "knock on wood", it became an idiom.