"To be, or not to be, that is the question" is the first line of the soliloquy that Hamlet speaks aloud in Act III, scene 1, while Claudius and Polonius are hidden to spy on his meeting with Ophelia. Hamlet weighs whether it is worth enduring the pain of living and ends up speaking of action.
Hamlet asks whether nobility lies in enduring "the slings and arrows of outrageous fortune" or in fighting "against a sea of troubles." He then weighs death as a sleep: "To die, to sleep. To sleep, perhaps to dream: there is the snag." What holds him back is what may come after death, "the undiscovered country from whose borders no traveler returns."
In Act I, scene 2, he had already lamented God's law: "Or that the Eternal had not fixed his law against suicide!" Here, the obstacle he names is the dreams that may come in the sleep of death.
In the close, he links excess reflection to lack of action: "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 vigor and importance "lose the name of action." A detail: the image of Hamlet holding a skull and saying "To be or not to be" does not exist in the play. The skull, Yorick's, appears only in Act V, scene 1, long after the soliloquy.