He doesn’t. He’s what you sometimes hear referred to as the “exception that proves the rule.” Like how at the end of Hamlet, everybody dies. Except Horatio.
The official body count in the final scene (Act 5 Scene 2) of Hamlet is four: Gertrude, Laertes, Claudius, Hamlet. Enter Fortinbras, who says “What happened here?” and Horatio is left to tell the tale.
Kenneth Branagh’s 1996 film version may have also killed off Osric (the referee, for lack of a more description term), it’s difficult to tell. In Branagh’s version, Fortinbras is actively invading the castle while the final duel takes place between Hamlet and Laertes. Osric is seen being taken by surprise and stabbed. However, he then returns to the scene to deliver his line about Fortinbras’ “warlike volley.”
In some interpretations, such as Ingmar Bergman’s 1986 production, Horatio is killed at the end of the play. When Fortinbras orders, “Bid the soldiers shoot,” some directors have taken that as license to execute Horatio, presumably as the last remaining witness to all that had taken place. It’s important to note that there is nothing in the text to indicate this (just like Osric’s death above). However, there’s two ways to die in a play. Either the script says you die, or else you eventually just run out of lines. Once you’re no longer part of the action (such as Osric), you might fall victim to artistic license and find yourself dead at the end of Act 5 whether Shakespeare wanted it that way or not.