Why does Horatio consider killing himself and what does Hamlet say to stop him?
We don't precisely know why Horatio would want to commit suicide, but Horatio is a rationalist, and events are unfolding in such a way that ending his life could easily seem preferable to staying alive. Hamlet, his best friend, is dying, and the room is littered with the corpses of people Horatio knows well. Beyond that, an invading army led by Fortinbras is at the doorstep. His friends are dead or dying and his country conquered: this is not a hopeful scenario for him. His desire to drink the poison seems to arise from feeling no reason to stay alive.
Hamlet, however, wisely provides the logical Horatio with a rationale for surviving. Hamlet convinces him that his life has meaning as a witness to the events that occurred. He is the only one still living who can tell the true story of what happened from start to finish. Just as Horatio bore witness to the reality of the ghost early in the play, now he can bear witness in a reasoned and even-handed way to the tragedy that has befallen the royal house of Denmark.
Hamlet knows his friend and says that if Horatio loves him ("If thou didst ever hold me in thy heart"), Horatio will give up the happiness of an immediate death ("Absent thee from felicity awhile"), and suffer the pain of being alive ("in this harsh world draw thy breath in pain") in order to tell his best friend's story. The loyal Horatio obeys his friend's dying request.
The play doesn't actually make it quite clear what Horatio's motives are. But he sees it, just like a historical Roman, as more noble to commit suicide. When Hamlet says
Horatio, I am dead;
Thou livest; report me and my cause aright
To the unsatisfied.
Horatio disagrees -
Never believe it.
I am more an antique Romanthan a Dane.
Here's yet some liquor left.
As th'art a man,
Give me the cup. Let go! By heaven, I'll have't.
O God, Horatio, what a wounded name,
Things standing thus unknown, shall live behind me!
If thou didst ever hold me in thy heart,
Absent thee from felicity awhile,
And in this harsh world draw thy breath in pain,
To tell my story.
In short, Hamlet wants Horatio to be around to tell the story of why Hamlet did what he did, and exactly what happened. There needs to be a witness for history to judge him kindly. And what Hamlet doesn't realise within the play, but Shakespeare realises without it, is that history, is of course his story. The play - Hamlet - does exactly what Hamlet asks Horatio to do.