Ah, great question. Now, I cannot tell you hard and fast rules of how to write a joke. But I can tell you what I have done in my life. And if you like my comedy, then maybe this post will be useful to you. And if you hate my comedy, then you can learn what not to do.
The first thing is that you need to get some basics under your belt. You need to understand some simple joke structures. I'm not talking about universal rules of comedy. I'm just talking about understanding the mechanics of a few short (not that funny) jokes. Let's take an example:
Misinterpret a Phrase
"You and I are very similar.... they're both vowels"
This joke sucks. But it has a structure that you need to understand. It's called misinterpretation. "You and I" have two different meanings. The second part of the joke reveals that you were intending the meaning to be different than the common interpretation. Get it? Try and write a joke with the exact same structure to prove that you know what's going on. Pick a word or phrase that can have two meanings and then make the second part reveal that you are using the less common version. I'll give it a shot right now:
"I hate it when construction workers do shoulder work on the road... can't you work your shoulders at the gym?"
Another joke that sucks. But I literally wrote it just now in thirty seconds. The idea is that I would sit down and write ten of these and probably one of them would be good. Cool, let's try another structure:
Take a common cliche and give it a different ending! I'm not going to worry about being good. I'm going to worry about the joke structure.
"A journey of a thousand miles... is super long and I'd rather stay home"
"An eye for a eye and a tooth for a tooth... is some sort of weird barter system"
Why does this work? Because you set up an expectation that the cliche will end the way that it normally ends, but then it doesn't! You can literally say anything other than the original ending and it will be somewhat funny.
These are a really silly type of joke where the adverb at the end turns out to be a pun. Here we go:
"I hate lobster!", Tom said crabbily.
"I'm tired of holding this banner", Tom said as his energy flagged.
"I don't want to die", Tom said gravely.
This is such a specific type of joke that I love it. You probably would never tell this type of joke in a stand up act, but it's still important to know how it works.
So, I've only detailed three types of joke structures so far. If you're interested in many many more types, please take a look at Mel Helitzer's Comedy Writing Secrets. This is the first book I ever read about comedy. Now let's be clear. After you read this book and write a bunch of jokes based on what you've learned. The result will be BAD COMEDY. It might be ENTERTAINING given the situation that you are in, but Chris Rock is not going to take you on tour. But, with these basics drilled into your subconscious, you can guarantee that five or six years into the comedy grind you will be coming up with some pretty interesting stuff. The issue that many people face is that because they never got the basics, even six years down the line they are still churning out bs because they don't know any of the rules. Here's a set of mine from my first year in comedy. I think I had been on stage maybe 15 times total at this point. All of them at open mics. This video is from The Brainwash in San Francisco. Watch it and you'll see what I mean. It's bad comedy, but it's still entertaining. These kinds of sets sustained me for my first years in comedy.