TL;DR: It went against to the message the writers were trying to portray and wasn't important to the story at that point in time so they left out showing how many were still alive.
It's worth noting that of the half that are gone this applies to the Northern, Arryn and Unsullied forces. It’s not mentioned how many Dothraki survive, only that some do.
Grey Worm: Half are gone. Grey Worm removes half of the Unsullied markers.
Jon Snow: The Northmen as well. Jon Snow removes half of the Northmen markers.
Yohn Royce removes half of the Arryn markers.
Dothraki soldier removes half of the Dothraki markers.
[...]
Tyrion Lannister: So if all are in agreement Jon and Ser Davos will ride down the Kingsroad with the Northern troops and the bulk of the remaining Dothraki and Unsullied. A smaller group of us will ride to White Harbor, and sail from there to Dragonstone with our queen and her dragons accompanying us from above.
Game of Thrones, Season 8 Episode 4, "The Last of the Starks"
Now in universe they were certainly still fighting inside the castle somewhere. We are shown small point of views from the main characters and don't see the majority of the battle and what goes on. It makes sense that we miss a lot of what actually happens. They survived because their battle happened off screen and away from what we were supposed to see, for all we know around the next wall there was still a few thousand vs the wights.
The Unsullied are also "locked out" of Winterfell when Grey Worm pulls the bridge and so it is possible that some of them were still fighting around the castle grounds against wights in pocket groups here and there.
Out of universe this makes sense as well, the writers wanted to convey the message of all hope being lost and so wouldn't show a huge amount of the army left because that moves away from the message they are trying to send.
How did Daenerys' army survive that much?
Well... it didn't — that is, until the writers needed some of the army back, obviously ;)