During Shakespeare's time, people believed in the Great Chain of Being.
This was a strict hierarchical order that ruled over every living and non-living object in the universe which was ordered by God. According to the Great Chain of Being, all existing beings have a specific purpose in the universe and any disruption to the chain leads to disaster. Due to the Great Chain of Being, it was commonly believed that those who commit suicide would be punished after death. Hamlet referenced this in Act 1 in that he is so heartbroken that about the death of his father and his mother's lack of grief that he curses God for having "fixed / His canon 'gainst
1.2.134) for he no longer wants to live with his sadness. He exclaims "O, that this too, too, sullied flesh would melt, / Thaw, and resolve itself into a dew," (Ham. 1.
2.133-134) to show that. By Act 3, Hamlet attempts to determine whether it is easier to suffer in the afterlife or suffer through life ("Whether 'tis nobler in the mind to suffer / The slings and arrows of outrageous fortune, / Or to take arms against a sea of troubles / And, by opposing them, end them.") While he does not consider suicide an answer to his problems, he recognizes that the only reason those do not commit suicide is due to their fear of the unknown.
In this soliloquy, when examining death Hamlet compares death to sleep ("To die, to sleep-" (Ham. 3.1.68)) and the relief that comes from it. However, he then dives deeper into his metaphor stating "To sleep, perchance to dream.
Ay there's the rub, / For in that sleep of death what dreams may come," (Ham. 3.1.73-74).
He realizes that with death comes the risk of the evils could come with the afterlife or the "dream." To Hamlet, it is not death that scares him, but rather the uncertainty of what follows death. Hamlet is frightened by what will happen to him after death and even by what may not happen. At this point in the play, Hamlet has transitioned from his mindset of wishing that he could commit suicide out of an act of desperation to one of looking at the nature and reasons for suicide pragmatically. He is no longer miserable with the fact he cannot escape his sorrows through suicide but rather he has accepted that killing himself is out of the picture. Due to Hamlet's examination of death, Hamlet's sadness has shifted into detachment from reality.
While he is still curious about what happens after death, he is no longer affected