Nepene Nep offers a good answer if your campaign is more of a combat-focused dungeon crawler, but if you’re more roleplay and character focused, there’s a handful of things that need to happen here:
As part of your preparations, make sure your players have a backup character concept. They don’t need to have a full sheet, but having that concept waiting in the wings can help prevent the “random magic NPC” issue. This gives the player something to do while the rest of the session wraps up, and, crucially, gives you as the DM an idea of where the new character can be plopped into the story! If all the stars align, the player might be able to get back into the game within the same session, or at least early in the next one, although I don’t always recommend this.
I try to keep things short and sweet after moments of emotional intensity, and a situation where a PC dies is almost always going to be emotionally intense. Even if your players aren’t really roleplaying the grief of losing a friend permanently, they’ve still probably been through some brutal combat. That takes it out of a person! This is why I especially recommend giving the player whose character died space before getting them back into the game; it can be a bit of an emotional ride for the players!
Give the living party members time to escape the danger and a bit of time to process the loss, and let the player whose character just died stick around to observe (and potentially to start filling out the sheet for their next character, if there’s not a lot of tearful eulogizing going on). Players, in my experience, tend to be nosey and to want to know everything that’s going on; even if the bereaved player is just watching the rest of the party, they’re unlikely to be bored. (Assuming that your players are generally a cohesive and friendly bunch and don’t have any other problems floating around.)
Again, I really emphasize the importance of giving players a relatively quick wrapup to a session where things go horribly wrong! In my experience, trying to continue with too much gameplay in the same session as The Awful Bad Event is exhausting to invested roleplayers. Get them to relative safety and then give both the characters and the players a moment to recover. This might mean cutting a session a little shorter than you’d normally go, but it’s worth it to make sure everyone is in the right headspace to continue adventuring.