{the question}
While Hamlet's famous "to be or not to be" soliloquy questions the righteousness of life over death in moral terms, much of the speech's emphasis is on the subject of death, even if in the end he is determined to live and see his revenge through.
Before engaging in the soliloquy itself, however, it is important to consider Hamlet's lines that occur before the passage in question.
{the meaning}
Hamlet curses God for making suicide an immoral option. At this early point in the text it is clear that Hamlet is weighing the benefits versus drawbacks of ending his own life, but also that he recognizes that suicide is a crime in God's eyes and could thus make his afterlife worse than his present situation. In essence, many of Hamlet's thoughts revolve around death and this early signal to his melancholy state prepares the reader for soliloquy that will come later in Act III. When Hamlet utters the pained question, is it better to be alive or dead? Is it nobler to put up with all the nasty things that luck throws your way or to fight against all those troubles by simply putting an end to them once and for all? there is little doubt that he is thinking of death.
Although he attempts to pose such a question in a rational and logical way, he is still left without an answer of whether the "slings and arrows of outrageous fortune" can be borne out since life after death is so uncertain.
{the afterlife}
At this point in the plot of Hamlet, he wonders about the nature of his death and thinks for a moment that it may be like a deep sleep, which seems at first to be acceptable until he speculates on what will come in such a deep sleep. Just when his "sleep" answer begins to appeal him, he stops short and wonders in another of the important quotes from Shakespeare's Hamlet, "To sleep: perchance to dream:-ay there's the rub; / For in that sleep of death what dreams may come".
The dreams that he fears are the pains that the afterlife might bring and since there is no way to be positive that there will be a relief from his earthly sufferings through death, he forced to question death yet again.
{the resolution}
After posing this complex question and wondering about the nature of the great sleep, Hamlet then goes on to list many sufferings men are prone to in the rough course of life, which makes it seem as though he is moving toward death yet again. By the end of this soliloquy, however, he finally realizes, "But that dread of something after death, / The undiscover'd country, from whose bourn / No traveler returns-puzzles the will / And makes us rather bear those ills we have". Although at this last moment Hamlet realizes that many chose life over death because of this inability to know the afterlife, the speech remains a deep contemplation about the nature and reasons for death.