This passage focuses on the human condition of despair and the dangers and fears of what lies beyond death.
In this most famous of his soliloquies, the fourth of the play, Hamlet ponders the existential state of man after talking with the ghost of his father and learning of the treachery of Claudius. He has also just discovered that his friends Rosencrantz and Guildenstern have come to visit not because they wish to see Hamlet but because they have been summoned to spy on him. With great melancholy resting upon his soul, Hamlet meditates darkly on the "fardels," or burdens, of living. Moreover, this concern with death overpowers Hamlet; he becomes preoccupied with being dead as he moves in his thinking toward the idea of "the dread of something after death" (3.1.77), which might be far worse than the "weary life."
Clearly, Hamlet is in despair as he expresses his weariness in living. And it is only his fear of the
...undiscovered country, from whose bourn
No traveller returns... (3.1.79-80)
that prevents his suicide. He finds too that thinking and being preoccupied with thoughts of death and life lead him nowhere. These meditations upon death lead only to trepidation and inaction.