Writing a campaign
A typical D&D campaign is created around the interplay between 3 things: The BBEG(Big Bad Evil Guy) who serves as the game's antagonist, The Macguffin, which is the item the BBEG is trying to obtain, and a Calamity, which is the terrible consequences that will take place if the BBEG succeeds.
The BBEG: Big Bad Evil Guy
As a DM, the BBEG is your main character. Your campaign isn't about the players. They will make their own story inside your campaign as they play the game. The story that you are writing takes place behind the scenes and focuses on the Villain of the story. For most campaigns, the BBEG has a network of henchmen and minions such as:
The BBEG: Acting in shadows, surrounded by mystery, this NPC is largely unknown to the players for the start of the campaign, and might not even be revealed until the final battle. They are intent upon obtaining the MacGuffin and causing the Calamity.
Number 2: The BBEG's right hand man, first mate, spouse, strongest creation, etc. This NPC is the most important to the game. They will be the face of the enemy for most of the game. Players may even assume or get the impression that they are the BBEG. The Number 2 carries out the orders of the BBEG and is completely loyal.
Henchmen: These NPCs are mini-bosses throughout the campaing. They carry out the orders of Number 2 and harrass the party during adventures. Each story arc is usually defined by meeting and eventually defeating a henchman.
Minions: These are your throw-away monsters. They are hired and commanded by henchmen and are only there so that the party can kill them during the adventure.
The first two things you need to do when writing a campaign is to decide upon a BBEG and their Number 2. Don't worry about the Henchmen and Minions yet, they will come later once you are planning your first story arc.
The MacGuffin
The Macguffin is the special item in the story that the Party of adventurers are trying to obtain, destroy, or prevent the BBEG from obtaining. There are several different types of MacGuffins and a single Macguffin can be multiple types.
The One Ring: This Macguffin is easily obtained by the party and must be kept from the enemy and eventually destroyed.
The Dragonlance: This Macguffin is hard for the party to obtain and is necessary to defeat the BBEG.
The Princess: Sometimes the Macguffin is an NPC that needs to be rescued or protected.
El Dorado: This type of Macguffin is a hidden location such as a burried city or ancient shrine.
The Truth: The Macguffin can be information, such as the true idenity of the heir to the throne, or a spell ritual.
The main importance is that the MacGuffin motivates the BBEG to act to aquire the MacGuffin.
Multiple MacGuffins:
It is common for a story to have multiple macguffins. One or more may be needed by the BBEG to cause the calamity, and one or more may be needed by the party to defeat the BBEG. These may overlap, having the party forced to bring a key item in the BBEG's plan to him in order to use it against him. If you run out of ideas for Macguffins, try rolling on the random Trinket table in the PHB p.160 for ideas.
The Calamity
The Calamity is the world-changing event that would be caused if the BBEG obtains the MacGuffin. This provides the motivation for the players to prevent the Calamity by preventing the BBEG from aquiring the Macguffin. Check out the section about World -Shaking Events in the DMG p.27 for ideas of what kind of calamities could happen. Not every campaign is epic. A group of heros could be trying to prevent a goblin chief from using a ring of earth elemental command to destroy a dam to flood a village, or they could be trying to prevent Orcus from harnessing the power of the Nexus Gem to turn Reality inside out.
Revealing the problem
Your BBEG is trying to get your MacGuffin in order to cause the Calamity, and you'll be revealing pieces of that information to the party. So your first Story Arc will result in the party discovering the following information:
Who is the Number 2: The party might not discover the identity of the BBEG at the end of the first ARC, but they should definatly know something about Number 2.
The Calamity is coming: This is the most important for them to discover at the end of Arc 1. It sets the mission for the entire campaign.
MacGuffin Plot Hook: The party might not learn of the MacGuffin yet, but they'll need a plot hook that leads them in the direction of the MacGuffin
Story Arcs
Now you need to write your first story arc. At the end of each story arc the party will be closer to securing the necessary MacGuffins and defeating the BBEG. You want your players to feel like that are making progress, either by obtaining more macguffins, or by learning more about the BBEG or Calamity. The first step to writing a story ARC is knowing what that progress is. For the first arc, it will be the initial revealing of the calamity and number 2. Your story arc needs these things:
A Henchman: This is your boss for the arc. They, along with some minions, will be a level appropriate encounter for your players according to your group's desired difficulty level. The Henchman will also have a number 2 and possibly a small macguffin and small calamity.
Setup: This is the beginning of the arc. The party needs to get a plot hook that will lead them to learn information about the henchman and why they need to defeat them. Even in long campaigns this part won't take more than a single session, and in a single session are may last only a couple minutes.
Investigation: The party plans and acts upon the information they found in the setup and learns more about the Henchman, his minions, and any other information they might need for the story to progress. Depending on your playstyle, this phase can a few minutes if part of a 1-session arc, or the investigation could cover multiple sessions.
Counter Attack: The Henchman's Number 2 has discovered the party's meddling and tries to stop them. This may be as simple as attacking them, or it might be more subtle. A hooded figure delievering a threatening letter could be enough. The party should learn enough about the henchman that they are now able to confront them.
Climax: This is the final battle or encounter with the Henchman, and is your boss battle for the arc. The party shoudl learn something about the BBEG, Macguffin, and/or Calamity before, during, or after this fight.
Wrap Up: This is the aftermath of the story arc when the party is rewarded with treasure and information. The party needs to discover their next plot hook that will take them to the next story arc
When writing a story arc, this is your opportunity to use your player's backstories. NPCs in a players backstory can have information about the henchman, or a villian from a backstory could be the henchman. A story arc can take as little as 1 session to complete, or it could take up multiple sessions. Not all story arcs will follow this template exactly. Some start with the counter attack, or have an additional investigation before the climax. As you practice, you'll alway be improving and learning different ways to run your campaign. This template serves as a starting point for new DMs and there are many other different ways to write adventures.
Random Encounters
Sometimes Dungeon Masters decide that their written story events aren't ready to happen yet, or they want to mix up the combat, or a player is absent and they don't want to do important story arc stuff. For whatever reason, DMs often use Random Encounters to generate story content. It is important to know the most effective methods for using Random Encounters.
Roll your random encounters during your prep, not at the table: This is a common mistake that new DMs make. Random encounter tables are best used during your preparations. A veteran DM with many years experience may be able to roll random encounters live at a table and run the resulting encounter effectively, but beginner DMs should not try to do that.
Separate your encounters into good and bad lists:This way, when you want to use a random encounter, you can call for a roll from the party, and depending on their roll, you can choose either a good or bad event. This keeps your events from feeling scripted.
Think of multiple possibilities for each event: Don't write a full script of the encounter before hand. Have two or three ideas of how the event might go. What if the players attack? What if they negotiate? What if they run?
Keeping Notes
Up to this point you've been preparing notes that you will use during the campaing. In addition to the information about the BBEG, Number 2, Macguffins, Calamity, Henchmen, and your current story arc, you also have probably been writing up descriptions of NPCs, designing towns, building dungions, designing traps, and stocking shops. These notes are important, but there are notes that are vastly more important to write down.
During the game, when you are playing D&D, you absolutely MUST take notes that record the events that take place in the game. These notes must be as detailed as possible and should include:
NPCs they meet: Your party is going to forget everything about this NPC in about 30 seconds after meeting them, but then they are going to want to talk to them again 5 sessions from now and you'll have forgotten everything about them too. Write down any important information shared by the NPC as well as any items that change hand and anything else you think might be useful. Don't forget the NPC's name.
Passage of time and events: A general play by play of game events is vital. You'll want to go back over this during your session prep before each game session to refresh your memory of the story so far. You'll also sometimes recap the entire campaign with this information. You're players will be amazed and it will greatly help with player immersion.
LOOT: What did that magic item do that the party got 4 sessions ago? They didn't write it down and you forgot. The only hope is to have it in your notes, so write it down.
You may ask how it's possible as a DM to take detailed notes while also trying to run the game as quickly and as smoothly as possible. The way I do it is quite simple. After a scene, I tell my players to roleplay the scene among themselves for a few minutes while I update my notes. I then take the time to record everything important that just happened while they chat.
These notes will be the most useful tool in your toolbox for improving your skill as a DM. They will provide inspiration for more story content, they help with story consistency, and help you learn from your mistakes as you go.
Have Fun
The most important rule in D&D or any TTRPG is to have fun while helping the rest of the group have fun as well. This means that you should respect the other group member's boundaries, and be a possitive influence. As the DM it is your job to set the example of respecful behavior and conflict management so that everyone, including yourself, can have fun at the table. If you have to choose between the integrity of your plot line, versus everyone having fun, I recommend choosing fun. Never fill a plot hole with an argument.