All you need is a stick. The carrot is a lie. *** In all some seriousness, the best humor derives from something happening that you had no reason to expect would happen. If somebody expects you to do something, you make them think that they're right, but then you change your mind halfway through after they've already gotten a specific expectation into their heads, then either they'll shoot themselves in the head with a chainsaw to get the "wrongness" out of their memory or they will laugh at and/or with because it was pretty funny. Humorists have been trying for decades to come up with jokes that would make test subjects do both, but unless you think that you're better than us, you should probably just settle for one or the other. Also, use the word "duck."
One thing I've noticed about the best humor writers is that they are among the most careful and precise writers out there. Humor writers have to care about their word choice, sentence structure, rhythms, etc. as much as lyric poets do. They revise, and they revise a lot. Look at the writings of the best humorists and you'll see what I mean. Check out the classics, like Thurber, Perelman, Robert Benchley. Even Dave Barry, whose persona was that of an immature, frat-boy goof, was a master of the precise turn of phrase, of placing the punch exactly where he needed to. (I use the past tense with Barry because he doesn't write his humor column any more - he's gotten more serious in his old age.) So be prepared to revise. It won't be funny unless you get it exactly right.
Humour is personal - not everyone laughs at the same stuff. There's enough people in the world that you may as well aim your writing at the ones who have a similar sense of humour to you rather than try to write something the generic 'They' will find funny. So work out what makes you laugh. Wordplay, slapstick, the unexpected, whatever. Find books you find funny and tear them to pieces trying to work out what bits get the chuckle and why they do it. Then practice writing things like that. Practice lots.
Start here: Melvin Helitzer - Comedy Writing Secrets. You may find a copy in your local library system. You'll get the background, the terminology, and the answers to questions you don't know you should be asking.
It's really handy if you have what's called a 'straight' character. Someone who the reader can identify with, and will contrast the funny things that the other characters do or say. The 'straight' character isn't necessarily normal, but his oddness usually comes through with more subtlety. Direction is also important. Like any story, laugh-riot or not, the story needs to keep moving at all times. John Dies at the End is a very funny book. It's a good example of a solid dark humour story, and doesn't betray you for investing yourself in it.
Not every person has a developed sense of humor. Even fewer can create humor that works in a literary medium. For example, most sight gags fall a bit flat in written form. Still, if you want to write humor, the same advice applies as with any other kind of writing - read the best and the worst examples, and learn what distinguishes them by first-hand observation.
there are so many approaches to humor, so many techniques. you can start with the greats as always- check out kurt vonnegut stuff, check out stand up comedian andy kaufman(not the movie!!). watch or read how they manouver the audience out of their dimension to theirs. There are the new stand up guys, Louie CK which is terrific, but I'm not sure his stuff will pass just by reading it. writing it probably need to be more articulated than just saying it aloud.
Grab a free range chicken (I don't care if you buy it, or hunt it down with a knife), just treat the animal with respect . . . wait . . . wrong forum. Sorry. Being funny isn't something that can be taught, but if you are funny than learning to write well is really all you need to do.
I think the key is reading it out loud. If you are doing dialogue you need to have someone read it with you. When you read something out loud you have reflexes to laugh or even giggle when it is funny, or a light will go off in your head when something doesn't sound right.