This famous soliloquy is from act 3, scene 1 of the play. At this point in the play, Hamlet is frustrated with himself for his inaction. He has heard from the ghost of his murdered father that the murderer was Claudius and has sworn to his father that he will avenge his death but has yet to fulfill the promise.
The soliloquy (a speech with no intended audience) begins with "To be or not to be, that is the question." Here, Hamlet is thinking about suicide and, more broadly, about whether there is any point or any inherent meaning to life. He then thinks about whether it is better to live and in so doing suffer the injustices of life ("the slings and arrows of outrageous fortune"), or to die and so put an end to those injustices altogether.
He continues, seemingly seeing more to recommend death than life, by saying that death is "a consummation / Devoutly to be wished." In other words, death is a final outcome to be wished for, and this is because it would allow one to "sleep." Hamlet repeats the word "sleep," suggesting that what he really wants is an end to the turmoil of his own thoughts. He wants to be at peace with himself again and with the world and to forget in death, as one forgets in sleep, the aforementioned "slings and arrows" of his life. His father has been murdered. His mother has been revealed to him as an adulteress. And he is full of self-loathing too for not having yet fulfilled his promise to his father's ghost.
However, Hamlet then realizes that this sleep of death may have a catch, namely the dreams that one may dream in death. He may here have in mind his murdered father, who in death still has no rest but walks as a ghost through the nights. Or he may simply be referring to the fact that death is so unknowable, and the possibility, therefore, that it may be even worse than life. Hamlet reasons that it must be a fear of death and the unknown of "what dreams may come" that keeps men living lives so full of "whips and scorns" rather than committing suicide to end it all. He then reels off a long list of injustices that he thinks are wrong with the world, including "the proud man's contumely, "the pangs of despised love," and "the insolence of office" and concludes that nobody would continue to put up with all of this but for "the dread of something after death," which "puzzles the will, / And makes us rather bear those ills we have / Than fly to others we know not of."
At the end of his soliloquy, Hamlet concludes that this fear and ignorance of death makes "cowards of us all." The implication is that it would take a brave man to commit suicide, not knowing what came next. He also concludes that thinking about something too much, as he has thought too much about death, weakens one's resolve to be brave and to act decisively. Before this soliloquy, Hamlet may have been determined to take his own life, but thinking too much about death has weakened his resolve and made him into a coward, not now brave enough to commit suicide. At least this is what he thinks. The implication is that he applies this same reasoning to his indecision in regards to fulfilling the promise he has made to his father's ghost. He has been thinking too much about whether the ghost has good intentions or bad, about whether Claudius really could have killed his father as the ghost described, and, of course, about what to do about his mother. All of these thoughts have weakened Hamlet's resolution "with the pale cast of thought" and made him "lose the name of action." This seems to mean, at the end of his soliloquy, that he is pushing himself to finally take action against Claudius.