The first reason that Hamlet gives for why people don't commit suicide comes early in the play, in act 1, scene 2.
HAMLET: O, that this too too sullied flesh would melt,
Thaw and resolve itself into a dew,
Or that the Everlasting had not fix'd
His canon 'gainst self-slaughter! (1.2.132-135)
Suicide is a sin, and in the highly religious environment of Shakespeare's England, this is a very serious deterrent. This is partially echoed in Hamlet's famous "To be, or not to be" soliloquy in act 3, scene 1.
HAMLET: Thus conscience does make cowards of us all. (3.1.90)
Not only is suicide a sin, but our own conscience tells us that it's wrong.
In the same soliloquy, Hamlet gives other reasons why people don't commit suicide, including the most elemental reason, which is fear of the unknown.
There's something odd about Hamlet's "To be, or not to be" soliloquy, which casts the common interpretation that Hamlet is contemplating suicide somewhat into doubt.
HAMLET: For in that sleep of death what dreams may come . . .
But that the dread of something after death
The undiscover'd country, from whose bourn
No traveller returns, puzzles the will,
And makes us rather bear those ills we have
Than fly to others that we know not of? (3.1.73, 85-89)
In act 1, scene 5, the ghost of Hamlet's father has returned from the "undiscover'd country" and gives Hamlet first-hand information about "what dreams may come" after death.
GHOST: My hour is almost come,
When I to sulphurous and tormenting flames
Must render up myself. (1.5.4-6)
If Hamlet's father isn't in Hell—generally considered a place where souls suffer "sulphurous and tormenting flames"—then he's most certainly in Purgatory, as "for a certain time" would seem to indicate.
GHOST: I am thy father's spirit,
Doom'd for a certain term to walk the night,
And for the day confined to fast in fires,
Till the foul crimes done in my days of nature
Are burnt and purged away. (1.5.13-17)
The Ghost doesn't tell Hamlet exactly what it's like where he is, but he gives Hamlet a pretty good idea that it's not somewhere that Hamlet would like to visit.
GHOST: But that I am forbid
To tell the secrets of my prison-house,
I could a tale unfold whose lightest word
Would harrow up thy soul, freeze thy young blood,
Make thy two eyes, like stars, start from their spheres,
Thy knotted and combined locks to part,
And each particular hair to stand an end
Like quills upon the fretful porpentine. (1.5.17-24)
Has Hamlet forgotten where the Ghost came from or what the Ghost told him? It seems unlikely, since the Ghost made quite an impression on Hamlet, and Hamlet vowed to seek revenge for his father's murder. If the Ghost can't even tell Hamlet what Purgatory is like because it would make his "hair to stand on end," what then must Hell be like, which is where people who commit suicide are condemned for eternity?
It appears that Hamlet isn't really contemplating suicide for himself in his "To be, or not to be" soliloquy, but he's simply theorizing in general about why people don't commit suicide.