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:
This phrase is used by people who rap their knuckles on a piece of wood hoping to stave off bad luck. In the UK, the phrase 'touch wood' is used - often jokingly by tapping one's head. The phrases are sometimes spoken when a person is already experiencing some good fortune and hope that it will continue - for example "I've been winning on every race - touch wood".
The derivation may be the association that wood and trees have with good spirits in mythology, or with the Christian cross. It used to be considered good luck to tap trees to let the wood spirits within know you were there. Traditions of this sort still persist in Ireland. See also - the darling buds of May.
The British version of the phrase - 'touch wood', predates the American 'knock on wood' and was itself preceded by a Latin version - 'absit omen', meaning 'far be that omen from us'. This dates from at least the early 17th century, when it is quoted by John Heywood in his collections of proverbs. It isn't clear when 'touch wood' began to be used as a token of good fortune but it must have been by 1850, when the academic correspondence magazine Notes and Queries published this:
There probably is some old English expression for averting evil, but it does not come to mind; "I touch wood," "Bar omen," "Bar ill-luck," seem clumsy.
'Knock on wood' is known from the early 20th century; for example, The Syracuse Herald, February 1905:
Neglecting to knock on wood may have been responsible for the weather's unseemly behaviour today.
Google Books shows a 1908 example: