So in response to Sean's question of how do you write funny, I have this to say: Use your toes.
But seriously, this is a really tough question, and I'm not sure there is a satisfactory answer. Humor is incredibly subjective and the line between funny for me and funny for you is thin one. There are jokes that I find hysterically funny that my wife barely laughs at and vice-versa. Likewise, delivery matters.
There are jokes that are funny when told by a master that aren't funny at all when an amateur tells them. There are jokes that are funny only because of very specific cultural contexts and jokes that are only funny when one person tells them. There are jokes tuned for gender, for age, for military experience. There is, in short, almost no such thing as a universal joke.
Medium matters too. I know people who are very funny in print but who aren't often funny in person, and vice-versa. That has a lot to do with context and subtext. When you tell a joke or make a funny comment in person, you're using a very different set of tools than when you do the same thing textually. Things that could sound quite mean or hurtful on the page can be delivered in person with the appropriate tone and body language to convey that the whole thing is really good natured. Just as something that might seem flat and overly subtle in person can be hysterically funny on the page.
None of which answers the question. I write humor almost as easily as I breathe. Even in my very earliest attempts at fiction I could often generate an out loud laugh in my reader. How do I do it?
Some of it is a talent for the funny in text, and I don't have any idea how to transfer it. Some of it is by creating characters with a long term friendly fencing relationship, so that the reader know that even the mean jokes are in the spirit of fun. Some of it is always directing the harshest jabs at my protagonist so that the reader gets it filtered through the (unhurt) eyes of my main character. Some of it is by making a lot of the humor self-directed. You're allowed to say awful but funny things about yourself that you would never consider saying about someone else. Some of it comes from always making sure that the characters are aware of the absurd in their situation--not by breaking the 4th wall and talking to the audience, but by realizing the inherent humor of being chased by a 600 foot firebreathing teal woodchuck.
Oh, and at least in my case, humor tends to be very very dark. A moderately funny bit in the midst of the world coming apart can be much funnier than the best joke ever while your characters are having a happy tea with no danger in sight.
Finally, a lot of it is hit and miss and fearlessness. You put the joke in and see who laughs. If you've got beta readers and writers group readers they'll tell you when you're funny and when you're not. You also understand that sometimes you fail with some readers, that something that's funny for one reader won't be for another and you accept that you can't make everybody laugh all the time.