I'm sure not everyone will agree, but I think the secret of writing humor is:
Don't try to be funny.
The best humor comes from observing people who don't think they're being funny. The classic sitcom, Fawlty Towers, is a great example. Basil doesn't see himself as a clown or a fool. In his mind Fawlty Towers is a wonderful establishment and he is a masterful hotelier. It's the rest of the world that laughs at him.
When you are writing about a character and putting yourself into his or her shoes, that's when not to try to be funny. If you have given the character a strong attitude, usually one at odds with the rest of the world in one way or another, the funny will come out. Then the dialogue isn't jokes, it's words that are funny only when they come out of the mouth of that character.
In my early sitcom days it was a gift to write for shows like "Benson" and "Family Ties," in which the characters were so strongly defined.
I was interested to read in an interview with Quentin Blake in the Sunday Financial Times that he has a similar attitude toward his drawings:
“The humour is a by-product [of the story]. You draw the scene, what people are doing, their reaction to it, and if it’s funny, it comes out. There are certain books where you play it for laughs but it’s always more interesting in a dramatic situation.”
I saw this in action years ago in a black comedy play I wrote that was produced in Los Angeles. The first week the actors played their roles straight and got lots of laughs. Then they began to anticipate the laughs and ham it up a bit. The director was new and unassertive and let it go. The reviews perfectly reflected this development: the ones from opening night were positive, the ones that were written later less so.
(For friendly guidance for your writing, get a copy of Your Writing Coach, published by Nicholas Brealey and available from Amazon or your other favorite bookseller. Consider ordering it from your local bookseller--we need to keep them in business!)