Cats most often knead when they're relaxed. Some speculate that it stems from kitten behavior -- kittens knead on their mothers while nursing. You might notice that your cat kneads more after long petting sessions.
If this behavior bothers you, simply place your cat nicely on the floor or his bed. You can also provide a soft area for your cat to knead and redirect kneading to this area or object. Don't punish your cat for kneading by saying "no" -- as you can make him fearful. Just keep an eye out for situations that precede kneading behavior, and try to avoid them.