The actual origin is still under debate as far as I know. But many experts believe it may have originated from Pagan belief systems. In the ancient past, trees were treated as being sacred for many reasons. In addition it was also believed that they were the homes to spirits, faeries and other mystical creatures. People who needed help would go to a specific tree. They would touch or knock on the tree and make their request for help/protection etc. They would then touch/knock the tree a second time to thank the spirits. The touching and knocking on wood was also believed to be a protection from evil forces and evil spirits. So in effect when we knock on wood or touch wood today we are trying to invoke good luck and keep away bad luck.