All of Hamlet's soliloquies are written in poetry because these speeches reveal his true thoughts. He puts on an "antic disposition" for others to see and speaks in prose when he plays his role, but in the soliloquies he expresses his real feelings and thoughts. Immediately after this soliloquy, Hamlet speaks in prose to Ophelia; this type of speech, which Elizabethan audiences could easily recognize, was a clue that Hamlet was pretending to be mad.
In the "To be, or not to be" soliloquy, Hamlet's decision not to kill himself comes because of one main fact: no one knows what comes after death. He notes that death can be like sleeping ("To die, to sleep/To sleep, perchance to dream...."), but the dreams may be nightmares. What would an eternity of nightmares be? Even more importantly, he decides that he is willing to endure the life he has ("grunt and sweat under a weary life") because of the fear of what comes after death, "the undiscovered country from whose bourn/No traveler returns." Melancholy and miserable though he is, Hamlet chooses life because he has no way of knowing what follows death.