This answer is an attempt to talk about the question on a purely mechanical level. Other answers have talked about how towns have fared against attacks by other monsters in official adventures, or speculated about how they think towns would consider the threat of dragons. But I don't think any have talked about how much of an actual threat dragons are to towns.
Towns are dangerous places for dragons
In 5e dragons are dangerous creatures for parties of adventurers. However, towns are large enough that they can seriously harm dragons through sheer numbers. I would expect dragons, who are quite intelligent, to be aware of this and keep their distance unless they were motivated to spend significant effort in sieging the town over a long term period.
In 5e an attack deals damage if it "hits" the target. To hit a target you must meet or beat the target's AC with a d20 roll, plus your hit bonuses. If a character managed to hide then they can get a bonus to this hit roll called "advantage" which lets you roll 2 d20s and select the highest. You can also get several bonuses to hiding if you can break line of sight, hide in the dark, try to hide when it's noisy, etc.
Then you roll damage, and add any damage bonuses. This is damage value is then subtracted from the enemy's hit points. Hit points are an abstract value representing a combination of luck, moral, will to fight, and physical durability. When a character's hit points reaches zero, the are knocked on conscious and could die.
However, a town wouldn't have to actually kill a dragon. When the dragon attacks the town, it's making a cost-benefit analysis: it wants something from the town, and it is willing to sacrifice something to get that. If the dragon is just hungry for a snack, it won't risk its life. If it's recovering a stolen dragon egg, then it will have a lot more on the line - but still it won't want to die. Generally speaking, I'm going to be talking about "unprovoked" dragon attacks, so I think hurting them by a quarter is going to be plenty; "what stops dragons from wantonly attacking random towns whenever they want?"
Raj points out in their answer that we actually have a lore example of a dragon interacting with Elturel - a city at least twice the size of a normal town. Here is the description of the interaction:
Years before the Companion appeared in the sky, a young red dragon
made life miserable for the residents of Elturel. It ate livestock,
burned crops, and demanded tribute.
It's interesting to note that the dragon did not attack the city directly, instead preferring to cause mischief in order to extract tribute. I suspect that this interaction is quite stereotypical of what we can expect. Dragons don't attack towns because they could die, instead they can safely harass the town from a distance. Of course even that isn't without its risks, a hero could show up, which unfortunately (for the dragon) is what happened here;
Then one day, a scruffy-looking, unarmored woman wielding a greatsword
marched up to the dragon and hacked at the beast with mighty swings.
Before the stunned creature could fly away, she slew it.
This is similar to the plot of Dragon of Icespire Peak. A dragon shows up, is a general nuisance, and is eventually slain by adventurers. Such is life in Faerûn.
The Mechanics of Attacking a Dragon
An adult red dragon is the archetypal dragon - big, red, breathes fire, flaps around on enormous wings. It has an Armor Class (AC) of 19, which is the measure of its defense. It has 256 Hit Points (HP) on average. It has a passive perception of 23, which means its rather good at spotting hiding people. Not only can it see in the dark up to 120 feet, it can also see-without-seeing up to 60 feet.
A commoner is a normal peasant. An average town in 5e has a few thousand inhabitants, most of whom are commoners.
A commoner has 10 in all stats, and a +2 proficiency bonus. For this example, the commoner will be armed with a sling (an extremely cheap, easy to make, weapon - ideal and accessible for a peasant) to enable them to make ranged attacks. Slings deal 1d4 damage, and can attack up to 120 feet - but anything over 30 feet will be at a penalty.
To calculate the odds of a commoner hitting the dragon, we can calculate like so;
- Odds of hitting = (21 - dragon's AC + attack bonus) / 20
- = (21 - 19 + 2) / 20
- = 4 / 20
- = 0.2
- = 20%
Not great odds to start off with. The sling deals 1d4 damage, so the average is (1 + 4) / 2 = 2.5. Multiplying it through, the sling will deal an average of 20% * 2.5 = 0.5 damage per turn.
The commoner will also score a critical strike if they roll a 20 to hit, so we can add a bonus 2.5 damage 1 in 20 attacks, so 2.5 / 20 + 0.5 = .625 damage per turn.
However if the dragon is more than 30ft away, then the commoner will be at a disadvantage, and have to roll 2 d20s and select the lowest dice. That means they essentially need both dice to hit to score a hit, lowering the odds to 20% of 20% = 4%, and the damage per turn to 0.106 per turn.
The commoner could try and hide behind something so they could attack when the dragon isn't paying attention. If they can do so then they roll 2 d20s and pick the highest, so the odds goes up to 36% to hit. To do so they would have to make a stealth roll that beats the dragon's passive perception. Unfortunately the commoner has a stealth bonus of 0, so it would be impossible for them to beat the dragon's 23 passive perception on a d20 roll.
However, there are ways for the commoner to lower that passive perception. If the circumstances are unfavorable to spotting the commoner then the passive perception is at disadvantage, which means it takes a -5 penalty. That would lower it to 18, which is beatable. Note that even though we talk about beating the passive perception, technically the passive perception is trying to beat the stealth check (which is a bit backwards by 5e's standards!), so you actually need to roll higher than 18, ie a 19 or 20. Some common unfavorable situations are breaking line of sight (so you have to rely on your hearing), being far away (it's much harder to spot someone hiding 100ft away from you!), being in the dark, being in a noisy area, general distracting circumstances, etc.
With this in mind, the odds of beating the dragon's passive perception is:
- Odds of hiding = (20 + Dragon's passive perception - stealth bonus) / 20
- = (20 - 18 + 0) / 20
- = 2 / 20
- = 10%
The commoner could also hide in circumstances which are favorable to being stealthy, in which case they get to roll 2 d20s and pick the highest. There's a lot of overlap between these and the circumstances in which it's hard to spot someone. If they can do it, then the odds increase from 10% to 19%.
Now we can calculate the damage per turn in each situation:
||Dragon is far away
||Dragon is within 30 feet
|Hiding in a spot that's hard to see
|Hiding in a spot that's easy to hide in and hard to see
Now keep in mind, in a normal sized town, 20% are children, 10% are elderly, half of what's left are women, and the other ~2000 people are men of fighting age. Some of those men are out of town, some of them are in the fields, some of them are sleeping, some of them are on the john, some of them are blind drunk, some of them lost their slings, some of them are out of range of the dragon, some of them don't have line of sight, some of them are just too far away. Let's say that by the time the town alarm has been raised and the dragon has approached, 5% of those men are in range, armed, and sober. That's 100 men. Remember, as the fight goes on more and more will turn up.
They will deal between 10 and 70 damage per turn depending on best or worst case. As I talked about above, the average dragon is going to think twice when they are a quarter dead (personally I think twice the first time I take a hit in a fight, but dragons are made of sturdier stuff). 256 (a dragon's average HP) divided by 4 is 64. That means between 6 and 1 turn depending on if the commoners are in the worst possible position, or the best possible position - if there's a mix of all 6 possibilities then it's 1.5 turns.
The dragon has between 6 and 30 seconds to realize they are in peril and get out of there.
Who else can live in the town besides commoners?
Commoners are just the lowest of the low. Practically any town will have all kinds of guards, thugs, archers, knights, etc.
I will talk about the situation from a purely mechanical standpoint. FR is a 5e setting, and you will find that the 5e rules apply. It's not clear to me if the events of Icewind Dale are suppose to be reverse-engineered to calculate the strength of dragons by using a super-weapon construct made of magical diamonds and infused with demon energy as an analogue for how normal towns interact with normal dragons, just because it superficially resembles a dragon. Instead, we are provided with stat blocks for various common people, and for the dragons themselves - therefore we will construct the situation forward instead of in reverse.
Generally if a dragon attacks a town, I would assume the defense would consist of everyone grabbing ranged weapons, and trying to drive the beast off. Generally people would try to hide behind whatever cover they could, and then pop out to throw rocks, use a sling, or maybe a bow.
The makeup of a town is highly variable, and there's no standard. But I'll talk about several common types of people in the towns. I'll spare you the math but I ran the odds vs an adult red dragon to see their hit chances with/without hiding, and how many hits it would take for them to kill a dragon.
Keep in mind, the goal is NOT to kill the dragon - if the dragon is taken to bloody (50%) or even 75% hp, it's going to have to seriously think twice about attacking the town.
I've also multiplied each "person count" by 10, because a town will have quite a few people, and I wanted to make sure it was obvious that we aren't talking about 1 knight fighting a dragon here. What I mean by this is that instead of just a single person, there's 10 people all attacking the dragon. The names listed in this column are the names of characters in D&D which are common to urban environments.
The weapon column is what weapon that character is armed with, for example archers have long bows. That's the weapon they will be using to attack with.
||Rounds to bloody**
*Stealth is the best odds to hide, assuming they can get advantage on stealth and impose disadvantage on the dragon's passive perception (e.g. by being both far away and hiding behind an object). Also remember to think about when the DM would rule that they simply cannot be detected by the dragon - e.g. breaking line of sight plus the sounds of battle are too much for the dragon to use hearing, plus they are too far for blindsight.
** The number of rounds it takes 10 people to reduce the dragon to 50% hp, if they can hide a proportion of rounds based on the Stealth column. The people need to be in range for this total number of rounds.
I had to make quite a few assumptions to come up with these numbers, so make sure you think about them in context.
- There was 10 of each respective type of person. Most towns will contain a combination, maybe one veteran, a few archers, a handful of thugs, and a lot of commoners. I think it's fair to assume that there is a mix of people in a town of thousands.
- I armed the commoners with slings, but realistically many would have short bows for hunting if not for times of war. Slings are the weakest ranged weapon, and considering they are incredibly cheap I think it's reasonable to assume.
- I assumed they would be able to find advantage + disadvantage opportunities to hide. This may or may not be possible due to their distance from the dragon, the layout, the time of day, etc. They almost may suffer disadvantage if the dragon attacks at night and flies high in the sky.
- Ideally the people will be spread out around the town, so the dragon can't kill them in one hit. Usually people will be going about their lives when the dragon attacks, so although some may be clustered at taverns or markets most will be all over.
- The dragon will have to be in range for those number of rounds, if it managed to move out of range that lowers the damage output - but keep in mind that archers attacking from hiding will be able to hit at 600ft without disadvantage, so it's essentially impossible to escape without breaking line of sight. In a town of many thousands I think it's likely that the dragon will be within range to at least some people some of the time.
- The dragon is a genius and will have their own tactics, but so will the towns folk. In a world where dragon attacks are possible and devastating, knowledge of how to defend against them will spread fast - I would guess that "everyone throw rocks at it" is the baseline, which is effective for even relatively small towns of normal commoners.
- When the dragon will run from the fight depends on the circumstances. If 20 commoners with slings bring the dragon down to 90% hp in 6 seconds, is that enough to make the dragon fly away? Certainly if I got the impression I would die in a minute if I hung around I'd be inclined to back off. The dragon's risk vs reward calculations are unknown, if it's just looking for a feed it may go elsewhere, if it's trying to recover a stolen egg it will commit despite the injuries.
- Theoretically a dragon could commit to a siege if motivated enough. Attacking and destroying a building, but escaping before they are killed. After sleeping for the night, they can return the next day and destroy another building. If the town doesn't organize a defense this could lead to the town being destroyed over a long period of time. If the dragon is motivated enough to spend weeks or months attacking like this, there's little a town could do besides ask for outside help.
- A dragon could attempt to set the town on fire to try and stop people from hiding, but this would take significant time and leave the dragon vulnerable. The dragon can only breath a 60ft cone approximately once every 3 turns, enough to set part of 1 house on fire roughly every 20 seconds.
- I assume that anyone's first response when seeing a dragon is to hide. If not, it takes an action to hide. The dragon can also use its legendary detect, but it's up to the DM if the dragon can detect everyone at once or has to search for each person or each area separately. If the dragon doesn't spot you, then you can freely continue to attack, as even if the dragon learns your location, so long as they can't detect you, you can continue to attack as much as you want. The dragon will be overwhelmed with targets so it can't fly up and attack everyone individually!
- The question was asked: why would a dragon be worried about being beaten half to death? Well, the answer is that the dragon doesn't know the makeup of the town. You may be inclined to think "well if it takes 15 seconds to beat the dragon half to death, the second half will take another 15 so there's plenty of time to escape", however what if at that point a party of adventurers reveal themselves? What if a rival dragon flies past? What if the dragon's lair is sieged? It's incredibly risky to routinely reduce your hit points to half. Remember that hit points in 5e are not just mechanics, they represent a character's luck, morale, as well as physical durability. Monsters should react to their HP depleting, because they have to live it. A dragon with half its morale and luck gone, is a dragon that is not going to want to stick around in the first place!
Dragons in 5e aren't invulnerable gods. 100 commoners slinging rocks can bloody an adult dragon in 12 seconds. A city like medieval London with 100,000 people? Forget about it, that would be certain death. This is something that any dragon will be aware of. They will likely not attack a town without good reason to do so, and not without being careful.
But for small hamlets, there's nothing they can do but offer tribute and hope there's no reason for the dragon to attack. Dragons, for their part, would know that if they wantonly attack villages then a few towns could easily send a raiding party to the dragon's nest.
The nitty gritty
I initially wanted to avoid posting too many numbers, as it will bog down the answer. However I have had a request to thoroughly explain the answer in depth so those who are less familiar with 5e can follow along. Apologies for the "broken" links, I don't have enough rep to post many links.
Here are the statistics for an adult red dragon.
I have evaluated several different types of NPCs common to the Urban environment:
I then calculated the chance of hiding for each type of person. As per the rules for hiding we should calculate the dragon's passive perception as 10 + their perception, in this case 13, giving a total of 23 passive perception.
Anyone who tries to hide must be able to roll a stealth check in excess of this number. I don't want to assume the townsfolk are being excessively tactical, but I think it's reasonable to assume that the townsfolk are capable of basic actions like ducking around a corner. The rules for advantage/disadvantage state the DM can decide to apply either as appropriate. As per the vision and light rules dim light imposes disadvantage on perception checks. We can also hide behind corners to impose disadvantage, and most DMs would probably rule that being completely out of sight is advantageous to stealth. It does depend on the DM (which I presume is you) so think about what situations would make stealth easier. Also note that according to the rules for passive checks, a passive check at disadvantage is at a flat -5.
Chance to pass a check can be calculated as follows: 1 - (DC - 1 - bonuses) / 20, this value should be bounded between 0% and 100%. You can find more info here.
- Commoner: Stealth +0, chance to hide 0%, chance to hide if dragon has disadvantage 10%, chance to hide of dragon has disadvantage and the commoner has advantage 19%
- Guard: +1 stealth, 0%, 20%, 36%
- Archer: +4 stealth, 10%, 35%, 58%
- Knight: +0 stealth, same as commoner
- Thug: +0 stealth, same as commoner
- Veteran: +1 stealth, same as guard
Next is to make the attack, that requires an attack roll and then a damage roll if it hits (scroll down a bit). Thanks to hiding, you can also gain advantage which will increase your chance of hitting.
Now we can use these values to calculate average damage;
(chance to hide * odds of hitting with advantage + (1 - chance to hide) * odds of hitting without advantage) * damage
The dragon has AC 19 so that's what we are rolling against. The average damage of a weapon can be calculated as (min value + max value) / 2 + bonuses
For simplicity's sake, when calculating the odds of hitting, we can set the minimum value to be 10%, and add 5% to all other values to represent the chance of a critical hit - which deals roughly double damage (if you consult the stat sheets for each NPC you will see that they have low bonuses, so this will save us from having to double all our damage calculations, which is fine because the numbers are just indicative in the first place).
- Commoner: (chance to hide 28% * odds of hitting with advantage 28% + (1 - 28%) * odds of hitting 15%) * average damage 2.5 = ~0.5 damage per round
- Guard: 1.3
- Archer: 5
- Knight: 1.7
- Thug: 1.7
- Veteran: 2
Since we aren't talking about a 1v1 fight here, I multiplied all the values by 10 to represent 10 people attacking for a round:
- Commoner: 0.5 damage per round * 10 people = 5 damage per round collectively
- Guard: 13
- Archer: 50
- Knight: 17
- Thug: 17
- Veteran: 20
Now you can take those values and decide when the dragon will have second thoughts. I think that a dragon that is randomly attacking a town will probably think twice fairly quickly, so I think 1/4 hp is a good metric. Imagine if you randomly decide to do something, and become severely injured within seconds - I would think twice!
So the dragon has 256 hp on average, 256 / 4 is (come on, you know your binary!) 64. So now we divide that through by the number:
- Commoner: 64 hp to think twice / 5 damage per round = 12.8 rounds until the dragon thinks twice
- Guard: 5 rounds
- Archer: 1 round
- Knight: 4 rounds
- Thug: 4 rounds
- Veteran: 3 rounds
Aren't people afraid of Dragons?
Adult dragons have an action called Frightful Presence.
Each creature of the dragon's choice that is within 120 feet of the
dragon and aware of it must succeed on a DC 19 Wisdom saving throw or
become frightened for 1 minute. A creature can repeat the saving throw
at the end of each of its turns, ending the effect on itself on a
success. If a creature's saving throw is successful or the effect ends
for it, the creature is immune to the dragon's Frightful Presence for
the next 24 hours.
We can calculate the odds of a commoner beating this DC 19 check by (21 - DC 19) / 20 = 2/20 = 10%. However the save can be repeated each turn, so on the second turn there's another 10% chance for them to make the save. We can calculate the total chance that a commoner makes the save by the first turn by adding the 10% from the initial save, to the chance of failing the initial save (90%) multiplied by the chance to save on the first turn: so 10% + 90% * 10% = 19%. We can continue this pattern:
||Cumulative odds of resisting frightful presence
So on average, the dragon buys itself ~30 seconds on average by using Frightful Presence. For those who are affected, the fear effect doesn't inhibit their ability to hide (since it only works if they are in line of sight, and they will likely break line of sight to hide), and the effect will be neutralized by the advantage for bonus. Worst case, they run away for 30 seconds.
This ability is very strong in fights against adventurers, but against a town it's weak. The radius is small, and the dragon will have to be close to the ground to maximize that radius. There's nothing stopping people from simply hiding until they resist the save. It uses the entire action of the dragon, and best case only lasts 10 turns.