The protagonist of Hamlet is Prince Hamlet of Denmark, son of the recently deceased King Hamlet, and nephew of King Claudius, his father's brother and successor. Claudius hastily married King Hamlet's widow, Gertrude, Hamlet's mother. Denmark has a long-standing feud with neighbouring Norway, and an invasion led by the Norwegian prince, Fortinbras, is expected.
The play opens on a cold winter midnight on "a platform before the castle" of Elsinore, the Danish royal castle. The sentries Bernardo and Marcellus and Hamlet's friend Horatio encounter a ghost that looks like the late King Hamlet. When it declines to talk to them they attack it with daggers, but it escapes. Marcellus admits: "We do it wrong... to offer it the show of violence / For it is... invulnerable." They vow to tell Prince Hamlet that his father's ghost has been seen.
The scene shifts to a "room of state in the castle." Various royal figures come in. Claudius and Gertrude talk with Laertes about his upcoming journey to France. Laertes's father Polonius admits that he has agreed to his son's trip. The King and Queen then turn to Hamlet. Perturbed by his continuing deep mourning for his father and his increasingly erratic behaviour, Claudius and Gertrude try to persuade him to be more cheerful. Claudius tells him that it is normal for fathers to die, but the prince is not comforted by this.
When they leave, Hamlet complains in his first soliloquy ("O that this too too solid flesh would melt") that his mother has jumped into "incestuous sheets" with her brother-in-law too quickly after the death of Hamlet's father. Horatio and the sentries come in and Hamlet warmly greets his friend, who has recently returned to court from the university at Wittenberg. The three tell Hamlet about the ghost they have seen at the castle and the prince resolves to see the apparition himself.
Claudius and Gertrude send two student friends of his—Rosencrantz and Guildenstern—to discover the cause of Hamlet's mood and behavior. Hamlet greets his friends warmly, but quickly discerns that they are spies. That night, the Ghost appears to Hamlet and tells him that Claudius murdered him by pouring "juice of cursed hebenon" in his ear, which caused his blood to "curd" and his skin to be covered with a "vile and loathsome crust." The Ghost demands that Hamlet avenge him. "Well said, old mole!" replies the prince, and he tells Horatio and the rest of his crew that he is going to "put on an antic disposition" from this point on and that if they run into him around the castle they should not say such things as "Well, we know," or "We could, an if we would," Or "If we list to speak," or "There be, an if they might," that would give him away. He is, however, uncertain of the Ghost's reliability.
Polonius is Claudius's trusted chief counsellor; his son, Laertes, is about to resume studies in France; and his daughter, Ophelia, is courting Hamlet. Neither Polonius nor Laertes approves of the match, and both warn her off. Shortly afterwards, Ophelia meets Hamlet secretly but is so alarmed by his strange antics that she tells her father of Hamlet's state. Polonius blames an "ecstasy of love" for Hamlet's madness and informs Claudius and Gertrude. At their next tryst, Hamlet rants at Ophelia, accusing her of immodesty and dismissing her to a nunnery.
Hamlet remains unconvinced that the Ghost has told him the truth, but the arrival of a troupe of actors at Elsinore presents him with a solution. He will stage a play, re-enacting his father's murder, and determine Claudius's guilt or innocence by studying his reaction. The court assembles to watch the play; Hamlet provides a running commentary throughout. After seeing the Player King murdered with poison in the ears, Claudius abruptly rises and leaves the room: proof positive for Hamlet of his uncle's guilt. Gertrude summons Hamlet to her bedchamber to demand an explanation. On his way, Hamlet passes Claudius in prayer. Claudius has just been talking to himself about the impossibility of repenting since he still had possession of the ill-gotten goods: state power, "my ambition," and sleeping with the queen he married. "There is no shuffling," he points out, " He talks to the state ("O wretched state!"), to his bosom ("O bosom black as death!"), to his soul ("O limed soul, that, struggling to be free, Art more engaged!"), to angels ("Help, angels! Make assay!"), and finally to his knees ("Bow, stubborn knees)". Hamlet then sneaks up behind them, but hesitates to kill him, reasoning that killing Claudius right after Claudius prayed and cleansed himself of his sins would send Claudius straight to heaven while his father is stuck in purgatory. After Hamlet unsheaths his sword and leaves, Claudius mutters that his praying doesn't seem to be getting anywhere.
In the bedchamber, a furious row erupts between Hamlet and Gertrude. Polonius, spying on the conversation and hidden behind a tapestry, makes a noise; Hamlet, believing it is Claudius, cries "Dead, for a ducat, dead!" and stabs wildly, killing Polonius. Hamlet then pulls aside the curtain and sees his mistake. He does not feel too sorry about this, saying only "Thou wretched, rash, intruding fool, farewell!" He then berates his mother for marrying Claudius for the sex. He claims her "sense" was "thrall'd" to her "ecstasy," and observes that "rebellious hell... can mutine in a matron's bones." The castle ghost suddenly pops his head in and gripes that Hamlet still hasn't killed Claudius yet and that he is annoying his mother even though he was instructed not to. Unable to see or hear the Ghost herself, Gertrude takes Hamlet's conversation with it as further evidence of madness. Hamlet leaves, begging the queen to stop having sex with Claudius, and suggesting that she can get used to this if she makes it a habit to abstain. Hamlet hides Polonius's corpse in "the lobby," and Claudius, fearing for his life, sends Hamlet along with Rosencrantz and Guildenstern to England with a note to the King ordering Hamlet to be executed immediately.
Demented by grief at Polonius's death, Ophelia wanders Elsinore singing bawdy songs. Her brother, Laertes, arrives back from France, enraged by his father's death and his sister's madness. Claudius convinces Laertes that Hamlet is solely responsible; then news arrives that Hamlet is still at large. Claudius swiftly concocts a plot. He proposes a fencing match between Laertes and Hamlet with a poison-tipped foil, but tacitly plans to offer Hamlet poisoned wine if that fails. Laertes will be given a handicap, and Claudius intends to bet on Hamlet so that if Hamlet dies the murder will not appear staged. Gertrude interrupts to report that Ophelia has drowned.
Two gravediggers discuss Ophelia's apparent suicide, while digging her grave. Hamlet arrives with Horatio and banters with a gravedigger, who unearths the skull of a jester from Hamlet's childhood, Yorick, causing Hamlet to contemplate the universal nature of mortality. Ophelia's funeral procession approaches, led by Laertes. He and Hamlet grapple by Ophelia's graveside, but the brawl is broken up.
Back at Elsinore, Hamlet tells Horatio that he had written fake letters addressed from Claudius to the King of England ordering Rosencrantz and Guildenstern to be put to death instead. A courtier, Osric, interrupts to invite Hamlet to fence with Laertes. With Fortinbras' army closing on Elsinore, the match begins. Laertes pierces Hamlet with a poisoned blade but in the scuffle they switch swords and Hamlet wounds Laertes with his own poisoned sword. Despite a warning from Claudius, Gertrude drinks poisoned wine intended for Hamlet and dies. In his dying moments, Laertes is reconciled with Hamlet and reveals Claudius's murderous plot. In his own last moments, an enraged Hamlet blames Claudius for his mother's death and manages to stab and wound Claudius with the poisoned blade, and finishes him off by forcing him to drink his own poisoned wine. Horatio attempts to commit suicide by drinking the poison but Hamlet swipes the cup from his hands and orders him to live to tell the tale. When Fortinbras arrives, Horatio recounts the story and Fortinbras, seeing the entire royal family dead on the floor, takes the crown for himself.