In the final act and scene of Shakespeare's Hamlet, the protagonist Hamlet rightly sees that if all die, including Horatio, no one will be left to explain what happened and how King Claudius's solely to blame. In his third-to-last speech, Hamlet notes that the courtiers who were there to observe the dual are now staring silently, pale and trembling, having seen their entire royal family slay each other without knowing any reason behind it. Hamlet wants Horatio to live to explain to the courtiers who is responsible for all of the deaths. More importantly, Hamlet's wishes are expressed clearly in his second-to-last speech. Witnesses have just seen him kill his own uncle and stepfather, as well as seen Laertes die from a wound Hamlet inflicted using a poisoned foil. In short, as Hamlet phrases it in his second-to-last speech, his own good name has been decimated; in other words, his own good reputation has just been destroyed, all because of King Claudius's own treachery, as we see in Hamlet's lines:
O God, Horatio, what a wounded name,
Things standing thus unknown,
shall live behind me! (V.ii.341-43)
Since Hamlet knows his own good reputation has just been slaughtered, he feels it is imperative that Horatio live to help clear up Hamlet's good name, or reputation. As Hamlet phrases it, so long as King Claudius's guilt and Hamlet's deed of just revenge remain unknown, Hamlet's good name will remain destroyed, as we see in his line, "Things standing thus unknown, shall live behind me!" (342-43). Hence, Hamlet pleads with Horatio that if he ever truly loved Hamlet, then he should remain alive to tell the story of Claudius's guilt and Hamlet's just act of revenge in order to clear up Hamlet's reputation.
It is really Shakespeare who is concerned about having Horatio stay alive to tell all the facts to everybody at court. All the principal characters will be dead when Hamlet dies. Shakespeare did not want his audience left with the feeling that nobody would understand the true facts. For example, they will not know that Claudius murdered Hamlet's father. Shakespeare's audience, of course, will know all this, having seen it and lived through it. But Shakespeare's audience will want to feel that Hamlet is totally vindicated for killing Laertes, Claudius, and Polonius, and for having Rosencrantz and Guildenstern executed by the English. Otherwise everybody at Elsinore will be left with a very bad opinion of Hamlet, and Shakespeare's audience will be left with an uneasy feeling about that ending. Hamlet can also be blamed for the death of Ophelia, since he killed her father and that caused her to have a mental breakdown and either commit suicide or not try hard enough to save herself from drowning. It is really King Claudius who caused all the trouble by killing Hamlet's father and later trying to kill Hamlet. As Laertes says with his dying breath:
The King, the King's to blame.
One of the main themes in the play is remembrance. It is what separates us from beasts. Hamlet realizes in the graveyard that after we die all that is left is how history remembers us; how we live on in others. Hamlet says to Horatio, "Thou livest, report me and my cause a right to the unsatisfied." "Absent thee from felicity a while, and in this harsh world draw thy breath in pain to tell my story." Horatio is both Hamlet's close friend and universal witness to the tragedy in Elsinore. As friend and witness Horatio holds Hamlet's story.
The Amleth legend as it comes to us from history is essentially diagetic. It is a story from a story teller, i.e., Saxo Grammaticus. What Shakespeare does is give the story mimetic expression. This is the power of theater and one of the reasons theater is also a major theme in the play. So at the beginning of the play, Horatio, the story-teller for the Ghost (Hamlet Sr.) is over-powered by the image of the Ghost itself. Hamlet's story moves from a thing of little remembrance to the world's stage. At the end of the play at the suggestion of Prince Fortinbras, it is Horatio who urges that, "this same be presently performed." Hamlet is then carried to the stage.
At the end of Hamlet basically everyone is dead and when the people walk in the court they will believe that the wrong man is at fault. In order to fix that, Hamlet wanted Horatio to stay alive because only then will Hamlet be able to not be judged for killing the King.