By this point in the play, Hamlet has been through quite a lot. He has found out his uncle killed his father; he is extremely upset over his mother's remarriage to said uncle; he has girl friend trouble with Ophelia; his friends, Rosencrantz and Guildenstern, have betrayed him; he has accidentally killed Polonius; and his uncle has sent him to England to be killed. Luckily he discovered the plot and rewrote the note, but all in all he is in a bad situation. At the start of Act 5 he has a brief respite from all of this in his conversation with Horatio and the two gravediggers. As macabre as it seems, the graveyard scene is a chance for comic relief in the play. Hamlet is quite pleased with himself for doing what he had to do to preserve his life, sending Ros and Guil to their deaths in England, and he is rather philosophical in discussion of what happens after death. He comes to the very logical conclusion that no matter who a person is in life, he merely returns to dust after death. Whether the dead person is a great man like Alexander the Great or a common man, they both end up being just more dirt in the earth. Hamlet has worried about death and talked about death throughout the play, but this is the first time he has seen it in such simple terms. This realization helps him take the actions he needs to take against Claudius. As death becomes more real to him -- like when he actually holds the dead skull of Yorick, a man whom he knew well-- he realizes that death is just death and that it comes for all.
The other important event of this scene is Hamlet's discovery that Opehlia is dead by suicide. He is devastated over her death and reveals how much he truly loved her. Unfortunately the declaration comes too late. He and Laertes establish themselves as adversaries in this scene, thus setting up the sword fight that Laertes and Claudius were plotting in the previous act.
Hamlet is actually more ready to act now than he has been for a good part of the play. He realizes that all one can do is be ready to act when the time comes because man can't control every aspect of his destiny.
In Shakespeare's Hamlet, if I were Hamlet in Act Five, scene one, I would feel as if my head were spinning—I would also be confused, devastated and angry.
Hamlet would have just returned from England where he knows Claudius tried to have Rosencrantz and Guildenstern deliver him to be executed. Having just arrived and in the company of Horatio, he has a moment's reprieve from castle intrigue when he recalls the days of his childhood, happily spent with his father's court jester, Yorick. He recalls climbing on the man's back, kissing him, listening to his songs and jokes; it brings a feeling of nostalgia for Hamlet, remembering uncomplicated days within his father's castle, untouched by misery and secrecy. (This—the gravedigger's scene—is the only source of comic relief in the play.)
This reprieve is short-lived when Hamlet sees the funeral party arriving. It does not take long to ascertain that Ophelia is dead. All of the sneaking and spying, which he resented of Ophelia would have disappeared, for regardless of those things, Hamlet still loved Ophelia. He must be in shock to realize that she is dead. When he tries to express his sense of loss, Laertes attacks him. This also confuses Hamlet. He does not know that Claudius has already poisoned Laertes' mind against him, plotting Hamlet's death. Hamlet is enraged when Laertes tries to strangle him in Ophelia's grave, but admits his love for Ophelia, and even for Laertes. Hamlet must not understand that Laertes would be angry at his father's death at Hamlet's hand, and that he might well blame Hamlet also for Ophelia's death, in losing her mind when her father is murdered.
Hamlet has lost his father. Now dear Ophelia is dead. His losses are great. He has only just reconciled with his mother, and no one else but Horatio is there to take his part. I would imagine he feels lonely and longs for the days when his family was whole and he could spend his time innocently with Ophelia. Loss, confusion, anger for the King's actions, and depression must reign over Hamlet when he compares life as it used to be with the life he has now. He must also be anxious, knowing the King will not stop his attempts to remove Hamlet from his life.