Second, damage is far from the only point favoring ranged. Being able to target opponents you can't reach (fliers, terrain, etc.), being able to focus fire, not being a front-liner to get attacked, much less chance of missing an action due to having to move, not having to disengage or risk an opportunity attack if you want to change targets for whatever reason.
Damage favors melee, the things you talk about favor range, that is why it is better to be awesome at both ranged and melee than it is to be awesome at one and not good at another. Also something you did not mention is fighting styles favor ranged over melee too.
But melee does more damage and it provides for more control through AOOs.
When you are 15 foot away from the enemy who is hiding behind something and you can shoot at them with a bow at disadvantage or climb over/around their hiding place and attack them with a sword for more damage it makes more sense to use a sword. Similarly when you are 15 feet away from a concentrating mage and you can do 1 more point of damage with a sword it makes sense to do that as it is 1 more point which could mean a higher DC on the save that follows assuming all else is equal.
When the wizard is throwing spells at you and you have the option of shooting him with a bow from 10 feet away or doing more damage with a sword from 5 feet, the sword along with the higher damage and the ability to force a break in concentration is be
Taslania, my human fighter (6th or 7th level at the time) battlemaster with Great Weapon Master, Sharpshooter and a 20 strength and 18 dexterity in studded leather armor snuck up and attacked a den of terrafolk. She snuck up along with the party Rogue (swashbuckler).
They assaulted the front door, killing 2 at the door with surprise. The cleric (the only other party member) came rushing up the first round. The three cleared the first room room, with Tas killing 3 more I think. As you noted she downed one with a melee attack and had no more in reach, she pulled out a dart and flung it across the room hitting one engaged with the cleric and doing +10 damage due to sharpshooter with +5 attack/damage due to using strength and thrown. Once first room was cleared (2 turns probably?) she went back to the door firing out at the flying pterafolk outside the compound from cover inside their stronghold while the cleric plugged the hallway against the others coming that way. After she killed all the flyers she dropped her bow, pulled out her warhammer (which is a backup weapon) and helped the cleric finish off the last ones on the inside.
The DM and players joked about how Taslania had random weapons strewn across the entire room.
Now instead of a Dex ASI and sharpshooter, could she have went PAM and Sentinel and been slightly better at melee or go CBE, DEX and SS and do slightly more damge at ranged (with a much shorter range)? Sure she could but in a party of 3 players and as the only one with extra attack niether of those made much sense. Moreover when they later got a sunblade she wasn't 3 feats down a path that made it no good to use. As it was she was able to wield it and still get the best part of GWM without having wasted a feat on PAM or most of a feat on Sentinel.