In his "To be, or not to be" soliloquy in act 3, scene 1, Hamlet revisits some of the thoughts that he expressed in his first soliloquy in act 1, scene 2.
HAMLET. O, that this too too sullied flesh would melt,
Thaw and resolve itself into a dew,
Or that the Everlasting had not fix'd
His canon 'gainst self-slaughter! O God! God!
How weary, stale, flat and unprofitable
Seem to me all the uses of this world! (1.2.132-137)
Hamlet decides that even though the world is weary, stale, and flat and living sometimes seems to serve no purpose, suicide is nevertheless a sin and therefore not a viable solution to his problems.
In act 1, scene 2, Hamlet is grieving his father's death, and he's distraught about his mother's marriage to his uncle. His emotions are raw, and he's alternates between calm and agitated. One minute he's talking about how loving his father was to his mother and how she doted on him, and in the next minute he's railing against her and condemning all women for his mother's behavior—"Frailty, thy name is woman..." (1.2.149).
A few days later, Hamlet is again contemplating suicide, but this time he's thinking about it rationally, reasonably, unemotionally, and in the abstract.
Hamlet's not going to kill himself. He's already decided that. First of all, as he noted in his first soliloquy, suicide is a sin. Secondly, if he kills himself, he won't fulfill his vow to avenge his father's death, a vow that he made to his father's ghost.
Even though Hamlet has decided against committing suicide, this doesn't prevent him from thinking about it or musing about life and death and life after death.
HAMLET. To be, or not to be, that is the question:
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 end them. (3.1.63-67)
The key words are "in the mind." These words move Hamlet's soliloquy from an immediate question of whether or not to kill himself to an intellectual discussion with himself about the nature and implications of suicide, not only for himself, but for people in general.
HAMLET. To die, to sleep—
No more—and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'tis a consummation
Devoutly to be wish'd. (3.1.67-71)
Who could blame anyone for wanting to be rid of all of the trials and tribulations of their life and simply put themselves to sleep? The question for Hamlet, though, is what happens after they put themselves to sleep.
HAMLET. For in that sleep of death what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause.... (3.1.73-75)
Who would endure everything that people are faced with in their lives, "the whips and scorns of time, / The oppressor's wrong, the proud man's contumely, / The pangs of disprized love" and all the other things on Hamlet's list, except because they're afraid to find out what really happens after death? It's a reasonable question, and Hamlet has a reasonable answer.
HAMLET. ...But that the dread of something after death
The undiscover'd country, from whose bourn
No traveller returns, puzzles the will.... (3.1.85-87)
Hamlet says that no one truly knows what happens after death. There are stories, myths, legends, imaginings, and religious-based beliefs about what happens after death, but nobody knows for sure.
Hamlet seems to think that it's perfectly understandable that someone faced with an unbearable existence would want to kill themselves, "When he himself might his quietus make / With a bare bodkin" (3.1. 82-83). The problem is that few people want to kill themselves without knowing what happens next.
For those who believe, as Hamlet does, that suicide is a sin, the hell that awaits them in the next world would likely be worse than the hell that they're enduring in this world.
HAMLET. ... And makes us rather bear those ills we have
Than fly to others that we know not of.... (3.1.88-89)
So we endure, says Hamlet.
HAMLET, Thus conscience does make cowards of us all,
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pitch and moment
With this regard their currents turn awry
And lose the name of action. (3.1.90-95)