Tripping the 'funny bone' can be a hard thing to do: it's often difficult to predict what will be amusing and to know what is appropriate and inappropriate at a particular time. Some of us 'have it'; others don't. I've heard that "you can't teach funny." I believe this is true to a certain extent: it just doesn't click with everyone. However, almost everyone can make a good joke every now and then, and learning to refine their humor is the challenge. While it is almost impossible to say what is funny and what isn't without live feedback, we can start to see some patterns behind the art form.
Lying at the crux of humor is expectation. Humor (read: successful humor) defies the expectations of the audience: there is a twist. Whether the twist defies large-scale cultural notions or just contradicts the immediate situation, this is where humor lives and dies. For example, a particularly deadpan comedian was telling a story about picking up milk from a local convenience store. He puts the carton on the counter; the cashier asks him "is that it?" The comedian responds "no, I want to buy this." Is it funny? It's hard to say. Part of the experience is the performer himself: his demeanor, the timing, the execution. The humor in this comes from a misinterpretation. We've all heard that cashier's response, referring to whether we want to purchase anything else. It's so mundane that it's difficult to give it a second thought. Think of how unfunny this joke would be should the cashier have said "are you content with putting the milk on the counter?" In this case, the cashier calls attention to what's about to come next, ruining the surprise and making the comedian's reaction the expected response.
Anyone who has studied any form of art knows that expectations change over time, which drastically alters what is successful and in good taste. Notably, the same jokes are perceived in different ways, e.g. the chicken joke. You know, "why did the chicken cross the road? To get to the other side." Chances are, you've never found this funny. How could anyone even consider this a joke? The answer: it was shocking when it was first being told. It is set up like a joke: a strange question is being asked. The audience asks why, expecting a punchline. The answer is a mundane, obvious answer: this causes that dissonance of expectations and reality that makes something funny; the joke was a subversion of other jokes. So, why isn't it funny now? Well, give it 100+ years of circulation and the punchline becomes expected as everyone has heard it. Furthermore, this joke is usually the first one that people learn. Children who first hear it have had little to no previous joke experience. They don't have a conception of what a joke sounds like, meaning that they don't understand that the chicken joke is undermining other jokes!
So, changing expectations (over time) drastically alter what we find funny and not. I'd like to share a more successful scenario than the aforementioned 'chicken joke.' Monty Python's Flying Circus was a '70's sketch show made by 6 English comedians. "Dead Parrot Sketch" was perhaps their most successful (at least, well known) bit from the show. It centered around a customer (John Cleese) attempting to return a faulty piece of merchandise to an unhelpful clerk (Michael Palin.) The faulty merchandise? A 'Norwegian Blue' parrot that the clerk had previously sold the customer, knowing full well it was already dead. The customer returns and engages in a very long argument. The clerk continuously contends that the bird is not dead and offers the customer strange exchanges. Hilarity ensues. If you haven't seen it, YouTube "Dead Parrot Monty Python," it will more than likely be there. The sketch was riotous and well-received. Cut to 20-30 years later, when every Python fan knows it front and back and expects it at every live show. The Pythons decided that a rewrite was in order to keep the sketch fresh and funny. The result? John Cleese walks in, wishing to return the dead parrot. Michael Palin's response? "Yeah, it's dead" [paraphrase.] Scene. Can you imagine if the original sketch played out like the latter? It wouldn't have been funny at all: there's no conflict and a perfectly ridiculous situation would've been wasted. However, when it is so expected that the argument will occur, cutting it short with a perfectly agreeable man becomes absolutely brilliant!
The key to being funny is breaking expectations. A preacher in a sermon...who has tourette's? A romance scene...in the middle of a battlefield? A hero's epic quest to save the world...from an adorable puppy? Some general advice for all artists is "write (or draw/compose, whatever) what you know." This is why comedians who become successful always start joking around about the airport: they don't drive themselves anymore. Seriously, take in the world around you: where is it boring and tired? Twist it! A strange reaction to a familiar interaction. A new setting for a normal person or business. The material is already out there, it's just looking for someone to make fun of it.