From the dictionary, it looks like it's some kind of ritual one does for luck, but I still cannot figure out when one would really knock on wood.
Knocking on wood used to be a way of warding off suspected impending bad luck, and now it's used as a tongue-in-cheek sort of phrase for when someone has had a lot of good luck, meaning that the person should be careful to avoid the bad luck which may follow. The Phrase Finder says:
It basically means, "I know I might jinx myself". It reflects the awareness that what I say might be "famous last words". Some have considered it a way of "breaking the fourth wall". Great for describing sports predictions. To "jinx" refers to the power that spoken words have of counteracting the very thing they predict.
How to knock on wood:
Goofus: When the Rangers sweep the Canadiens, I'm going to take a week off work to watch the finals!
Gallant: When the Rangers sweep the Canadiens -- knock on wood -- I'm going to take a week off work to watch the finals!