There's no "right" answer, there are major playstyles that hit the two extremes and then there's compromises in the middle. The ENWorld post Combat as Sport vs Combat As War illustrates two different end state playstyles - in "combat as sport" the GM never makes "level inappropriate" encounters (no matter how hard you have to twist logic to get there), with each combat being specifically a "fair" setpiece, and in "combat as war" whatever happens happens, whether that means the PCs win without even fighting by poisoning the enemies' water supply or whether it's a TPK because they're dunderheads.
Now, these are two extremes which usually people don't do a pure version of. 4e tended toward Combat as Sport and earlier versions tended toward Combat as War. Given D&D 5e's inheritance of all those playstyles, I think you should think about where you want to fall on that spectrum and then be consistent, and communicate to your players what your approach is so they have aligned expectations.
Most people I have gamed with would let the chips fall where they may, but would make sure and telegraph that it's coming. See How can DMs effectively telegraph specific dangers in D&D? for a question entirely about that. In this case I personally would have them come across various other "hastily abandoned" rooms, maybe run across a messenger or group sending or responding to the "everyone gather up in the main hall to kill intruders!" summons, etc. I often find that doubling down on in-game reality helps - all the bad guys don't just immediately know the wizard wants them and teleport to his side, right, there's a fair amount of alarm generation, people running to and fro, etc. to make it happen. Heck, there's probably one or two folks who are aware of but don't respond to the summons (are sick, or cowardly, or just don't like the wizard that much) that the PCs could find and interrogate. The wizard may have powerful allies he doesn't exactly trust to fight in the same room as him (evil folks are always out to advance themselves with a dagger in the back of their boss) so maybe he deliberately sends some out on a reconnaissance in force. All these can be ways for the PCs to learn that an ambush of overwhelming force might await them. The solution to that, however, is their problem. Smart tactics? Wait them out? Cloudkill scroll? Heavy buffing including fire resistance? There's a lot of options for smart players.
But if they ignore all that and eschew intel gathering to go with a "LEEEEEEEROY JENKINS!!!!" approach then it's OK to kill all the PCs to make their players smarter. As others have noted, you could also opt to capture them or "leave one alive to spread the word of your infamy" but in my experience players hate these as much as just getting wiped out, so I'm not sure if there's value there - depends on how epic of a campaign you're running and how much work you're losing with a new set of PCs. They're only level 2, so it's on the low investment side, once they've been playing a while they'll have had a lot of characters die. The first time needs more hugs but it's going to happen, may as well happen now.
Keep in mind that ambush doesn't have to be the NPC's reaction, it could be escape or something else, depending on their motivations. So don't feel forced into that as a decision, but if you want to make that decision it's a legitimate one.
Your party is in the middle of a classic movie plot, where the team has an initial setback caused by a failure to work together effectively. Take heart that these stories have happy endings.
Real feelings come first
Your players seem to be really sniping at each other, and you want to make sure there are no ill feelings after people step away from the table.
Many experienced players can really get snide or even downright nasty in-character, and then stand up from the table and laugh about it. Other players, though, might get their “real” feelings hurt by comments about their character’s performance.
Please keep an eye out for this. If your players are getting truly upset with each other as people, not just as party members, you should take a little break from the game. Have a little fun with each other doing something else, until everyone cools off. (Maybe watch one of those movies with the teamwork theme.)
So they really like your knight...
A common trope in the “Learning to work together as a team” plot is the wise outsider who helps the team overcome its problems (The Sphinx in Mystery Men, the Vision in The Avengers, Gandalf in The Hobbit/LOTR, etc.) In your case, it’s the knight who came to their rescue. So yes, use the knight to help solve your party’s teamwork problem.
In and out of game issues
You now have 2 voices to deal with the bickering issues: your knight for game issues, and your own voice, for out-of-game issues. If things seem to be getting personal, use your own voice and step back from the game.
“Sir Coach-a-lot”
You want the party to be able to stand on their own, but right now they are teetering. Let the knight coach them along until their are ready to act alone.
Your knight might tell the party, “I am really getting too old to be an adventurer. But you are such a promising team. If you can rise above your current problems, I feel you are destined to do great things. Should you so see fit, I will try to help. May I accompany you for you next journey?”
This lets you model a humble spirit of teamwork, and sets the expectation that the knight’s help is for a limited time. The knight’s goal is not to become the leader of the party, but to make them capable of leading themselves.
Coaching, not Commanding
Feel free to give general tactical advice in the voice of the knight. The battle at the city wall might have gone differently if the party had hit the enemy a little harder early on; the knight can explain how important it is to gain the upper hand.
In battle, keep the knight’s advice as general as possible, ultimately devolving to simple encouragement, a la, “You know what do do!”
You might give your knight a heart-breaking backstory themed failing to save a companion, and how he has worked to redeem himself. The moral of the story being: Everyone makes mistakes, sometimes leading to calamity. But resiliency in the face of adversity is the true test.
Model Resiliency
Your knight’s prime purpose is to change the tenor of the party to one where the characters support each other instead of blaming each other when things go sideways. Let him be almost relentlessly supportive of the other characters, and relentlessly humble about his own accomplishments.
When a character snipes at another (“Aren’t you ever going to cast a spell?”) he might simply rephrase it in a more positive way (“Your magic will turn the tide!”)
If a character does make a serious mistake, the knight can model dealing with it in a mature and humble way. If one party member starts laying into another for a mistake, the knight can intervene, “If you are going to blame someone, blame me. I promised I would help you work as a team, and I have not yet succeeded.”
Saying Goodbye
Keep the arrow of this plot pointed squarely at, “the knight will leave the party as soon as possible.” He will do so when he deems the party is ready. This should provide the party a feeling of pride, being ready to face the world on their own.
The other way to remove the knight from the party is to see that he meets a tragic end, perhaps sacrificing himself to save the party. This has the advantage of forcing the party to really stand on its own, but be aware it would permanently take away a good tool you have to influence the party.
Do whichever you feel is appropriate for your story. Just don’t feel you need to kill him to get him out of the party. The last thing you want is for his death to be the trigger that starts your party bickering again.
Best Answer
You say "TPK"; I say you have just been handed the perfect opportunity to invent a
terrible unfinished plot
that just so happens to require the PC's being alive and held as captives.If it helps, cackle evilly and break out the A-Team quotes when you take down the last PC. The players don't need to know you made this all up since the previous session.