This is a trope that has been around since… ever. In just about any action film, the protagonists continually encounter overwhelming piles of stormtroopers, orcs, aliens, cowboys, or German terrorists, and seemingly cannot get killed.
Movie makers and dungeon masters struggle constantly with the tension between proving that the legions of terror are a highly trained credible threat, and the heroes are so amazing and skilled that they constantly evade the certain death that is right on their heels. In D&D, if your monsters are not perceived as a credible threat, then it detracts from the overall tension of the story.
How does the original Star Wars span this problem?
Plot armor is handled nicely by A New Hope. The movie takes great pains to make the Imperials appear competent. Stormtroopers are introduced in the opening scene, where they blow away dozens of rebel British extras. Their battle prowess is augmented by cunning: They wipe out a Jawa Sandcrawler, but disguise their attack to appear as if it's Tusken Raiders (Luke, a native of Tatooine, is duped by this. Only Obi-Wan, ex-general and jedi knight, deciphered the ruse).
Even volleys of misses on the Death Star are explained away. While Luke and Han are busy high-fiving their escape, Leia notes they were clearly let go intentionally, because their escape was too easy. Grand Moff Tarken confirms Leia’s suspicion in the next scene. The stormtroopers were trying to miss so the heroes could be tracked. It’s an awful risk, but you can’t find a secret rebel base without breaking a few stormtroopers.
The Empire Strikes Back handles this well too. In the beginning of the movie, the Imperial Army routs the rebels during the battle of Hoth. In the cloud city, the stormtroopers are, yet again, placed in the awkward position of not trying to kill Luke or his friends while still shooting at them. Remember, the person giving orders has no compunction about killing admirals let alone the poor sap holding the blaster rifle.
In Return of the Jedi, three legions of the finest imperial troops are taken down by teddy bears with Stone Age tools. Oops. Well, two outta three ain't bad.
How can this apply to your D&D game?
When your characters fight a competent organized enemy, you are going to run into this problem. Your heroes’ constant success will slowly pull away from the intensity of the evil they are fighting. How awesome or capable can the evil force be if your characters just win all the time? This is especially difficult in 5e, with the concept of the adventuring day. Heroes are supposed to go through 6-8 encounters a day: that is a lot of successes. Just like in A New Hope, you might need to employ some additional narrative to help balance out this tension. Here are some ideas:
Hard or tricky encounters
The obvious answer to this problem is to crank up the difficulty. Encounters won’t be “medium”, instead try “hard”, “deadly”, or “why would you do that?” Creatures under your control will use terrain, weapons, and tactics to their full advantage. The monsters should also set up traps and ambushes for the unwary. Be aware that you are towing a thin line: if you or your players screw up, you're look at the business end of a total party kill. Let your players what you are planning, as it is unfair to pump up the difficulty without notice. That being said, there is a reward to this path: if the players start to adapt, they will improvise tactical and strategic ways of turning combat in their favor or avoiding it all together. As a good DM, let them handle it as they will (as long as it’s reasonable), you’ll all be better off for it.
Do some role reversal and allow your players to play the bad guys. Pick out some nice high level monsters, and let them go to town on the forces of good. This should be a small vignette of something that occurs when the player’s characters are not there.
I once gave the players stats for a group of demons (CR 10 to 15) and let them crush the guards (CR 1/4) and veterans (CR 3) that protect the king. It slowly dawned on my players that they will eventually fight these creatures. While the characters did not encounter these demons until the appropriate level, they feared them, and that is half the battle.
The bad guys should have plans...
And they should think bigger than just the current encounter. Remember, they are characters too, and they should have goals and objectives of their own. During encounters the malevolent legions of terror can be multitasking, and that can make their soldiers vulnerable to attack. For example, if the bad guys are escorting prisoners and come under attack by the characters, the enemy will have to divide their forces, one to engage the heroes, and the second to guard the prisoners. You can also take a page from Empire Strikes Back, maybe the big bad wants wants them alive.
The Heroes are just special
This is actually one of the core assumptions explicitly listed in the D&D 4e DMG, but strangely absent in 5e. In this assumption the heroes, for whatever reason, are considered better, luckier, or more skillful than the average NPC, and this explains their ability to succeed. You can use a bit of fluff to explain away the character’s success. Perhaps a divine force keeps them safe; maybe the fates bend the world (slightly) to their will.
This is used to great effect in the Wheel of Time series. Three of the main characters are considered “Ta’veren”, special individuals who can bend probability to their ends. They are often saved by their special nature. A small narrative at the beginning can save lots of explaining later, when the characters easily defeat the well trained legions of terror. This can spawn plot hooks of its own, what happens if the power begins to run out?
This sounds like work, can’t we just ignore this problem?
Absolutely! Ignoring or accepting this tension is perfectly fine. You can easily have an enjoyable, fun story with these tropes, something akin to serial TV. It all depends on the style you want in your campaign. If you want a more serious, dramatic campaign, then you should strongly consider some extra effort to make your bad guys at least look credible. If you are just cracking open a beer to kill the lich of the week, by all means bring on the legions of expertly trained, yet easily fallible terror.