In Hamlet, the "to be or not to be" is not about suicide. Hamlet would not be debating suicide here. He had debated it earlier, but the Ghost has presented him with purpose now: kill the king and send him to hell, thereby freeing the Ghost from Purgatory to heaven. If Hamlet suicides, both he and the Ghost will go to hell, and, worse, Claudius might go to heaven.
Rather, the monologue is a meditation on existence (the interrelated nature and meaning of life, suffering, and death). According to existential critic Rheinhardt Grossman in Phenomenology and Existentialism:
Two things keep Hamlet from committing a suicide: fear of death and uncertainty that waits for him after it and the wish to revenge for his father’s death. Vainness and confusion are two words which can characterize all his life. Even when he decides to revenge for his father’s death and kill Claudius he does not use his chance. He got lost in his eternal thoughts about useless life, sufferings and pain. He is not able to see the world in a new perspective and cannot get out from the web of fear, darkness and pain which he himself created.
The monologue is about activity vs. passivity in response to suffering and a society gone to pieces: "to be (active) or not to be (active)" or, conversely, "to be (passive) or not to be (passive)." Hamlet wonders if he can withstand all of misery that seems inherent in the human condition. Is there a point to suffering? Should he fight against it or just accept it as inevitable. Denmark is a prison: is there a point in fighting to escape it when the world outside its walls might be a prison as well? To him, the world is so corrupt, that life seems pointless. Should he give up hope?
The soliloquy might also be interpreted in terms of the after-life. It's about being damned to hell by committing revenge or not being damned. So, "to be (damned) or not to be (damned)." The problem of sending the Ghost to heaven and Claudius to hell will cause him to go to hell as well (murderers go there).
In the end, though, Hamlet resolves his spiritual crisis by giving this answer in response to his earlier monologue:
If it be now, 'tis not to
come, if it be not to come, it will be now; if it be not now,
yet it will come. The readiness is all. Since no man has aught of what he leaves, what is't to leave betimes? Let be."
Hamlet has readied himself for activity and for the afterlife in Act V. He has thus answered his own earlier questions.