The way I've always played D&D was that when your character was dead with no chance of resurrection, you built a new character and came back into the game at level 1. That's just the way me and my gaming groups have done it. Recently, I've started to pick up on the fact that not all gaming groups play the game that way, and it made me think about what the "goal" of a D&D game. Is the goal to level up? Is the goal to complete a module (or a series of quests, a campaign, or whatever term you use)? Is the goal to tell a good story? Is the goal to get gold? Seriously, what's the game all about, anyway? I'm going to try to convince you that it's about reaching level 20, but first I want to look at some of the other ideas.
First of all, I want to make it clear that D&D doesn't have to have a goal. It's a flexible game, and it's fun to play. That's reason enough for many people.
I should clarify that it's not reason enough for me, though. I've tried playing RPGs with no clear goal, and I invariably lose interest very quickly. It's a little silly, because the goals that do get imposed on a game are make-believe. There's an equal amount of purpose in a game with a goal and a game with no goal. I could "fix" the problem with a game with no clear goal by inventing my own goal and playing toward it. But the group energy is different, I guess, and games without a common goal tend to be less focused than a game with a common goal. I find it more fun to play an RPG with a clear end in sight.
Some players love the RPG experience for the roleplay and storytelling. I love a good story, I love exploration, and I love lore. The more player handouts I get, or independent research I can do, when I'm playing a module, the more invested I become. That's a very real part of the game, for me.
However, it never qualifies as a goal for me. It's not reason enough to play a game. I think it has something to do with the knowledge that I could just learn the lore by reading it. But throw an imaginary monster in front of me, and tell me that I can't read the ancient scroll until I defeat the monster, and I'm hooked.
Completing a module or a specific dungeon is a valid goal for me. But in order to do that, I need a player character. And that can get tricky when practically everything in the game world exists to kill my player character.
Death in D&D can be a common occurrence, depending on your Dungeon Master and gaming group. As a Dungeon Master, I tend to make a best effort first to kill player characters (that's the game, to my mind) and second to offer the players a route to resurrection when needed. Some players appreciate divine intervention to save their character from perma-death, and others decline the offer and prefer to roll up a fresh one.
I fall into the latter group, myself. I run characters into the ground. I'm merciless. If a player character falls into a pit, I'm rolling up my next character before the black pudding living there has even made itself known. A new character is a chance to learn a new class or subclass, or to experiment with a new build.
A "goal" is something that's definitive. When you attain a goal, you have obtained a thing that you can then no longer obtain (because you've already obtained it.) In D&D, there's a lot of stuff you can obtain, but it's a little arbitrary. Sometimes you need to obtain the parts of a broken magical rod, and then you need an orb, and then a robe, and then you have to stop an evil monster, and so on.
The one out-of-world goal for D&D are character levels. You retain those across adventures, at least within a "campaign", although some DMs (myself included) allow characters, with their levels, to be imported into their campaigns.
For me, the goal of D&D has always been, and more or less has to be, level 20.
The obstacle to obtaining the highest possible character level (20) is both inaction and character death. If you do nothing, you don't gain XP and so you don't level up. And in your quest for more XP so you can gain a new level, you encounter surmounting danger, and are more likely to die.
When you die and come back into the game with a new character at level 1, you level up fast because you're riding on the coattails of higher level characters. I've had level 1 characters breeze past several levels after just one combat, thanks to a collective earning of thousands of XP. But as your new character gains higher levels, the rate of growth normalizes a little, and you start having to really earn your XP.
Getting to level 20 isn't easy. You might think it would be. After all, by level 15 you've got superpowers that turn the tables and put the DM on the defensive. And yet the DM has also gained a bunch of new tricks, and the gap between levels is wider than ever.
Getting a character to level 20 is the goal of D&D. It may not be the same character you started the campaign with, but it's a character you've played from level 1, and that you've leveled up with good, honest (and some slightly borrowed from allies) XP. When you finally reach level 20, you know you've played a game. You've been challenged, accosted, probably killed a few times, maybe resurrected or otherwise intercepted.
Level 20 feels as amazing as you'd expect. You're legendary, not just in game, but in many ways in the real world. Not every D&D player ever gets to level 20. That's not a bad thing, and there's definitely a discussion that could be had about whether D&D should even bother going as high as level 20, but it's nice sometimes to get to the end of the class table. No more improvements are available. You've acquired everything that was laid out for you to collect. Not the (pretend) physical objects, not the (pretend) gold pieces, not the satisfaction of having saved (imaginary) lives, but the specific rewards you get for staying in the game.
That's the driving force behind D&D, for me as a player, and that's how I manage my games as a Dungeon Master.