A soliloquy is a dramatic device which allows a character to speak directly to the audience. Its Latin origin means "speak alone" and the convention is that the character should be alone on the stage or at least clearly isolated from the other characters. This makes a soliloquy different from an aside, where the character just "whispers" a few words to the audience, perhaps even in the middle of a dialogue.
A soliloquy allows the playwright to develop a character from within, creating more psychological depth and highlighting differences between the inner and outer self.
When we first meet Hamlet, for instance, he is defiant and sarcastic towards his mother and uncle/stepfather, who seem impressively patient with him. It is only when he is left alone and begins his first soliloquy "O that this too too solid flesh would melt," that we become aware of the suicidal despair, and disgust at his mother's conduct, that underlie his sardonic public manner.