The protagonist of Hamlet is Prince Hamlet of Denmark, son of the recently deceased King Hamlet and his wife, Queen Gertrude. After the death of the King, his brother Claudius proclaims himself King and hastily marries Gertrude. In the background is Denmark's long-standing feud with neighbouring Norway, and an invasion, led by the Norwegian prince Fortinbras, is expected.
The play opens on a cold winter night at Elsinore, the Danish royal castle. Francisco, one of the sentinels, is relieved of his watch by Bernardo, another sentinel, and exits while Bernardo remains. A third sentinel, Marcellus, enters with Horatio, the best friend of Hamlet, Prince of Denmark. The sentinels try to persuade Horatio that they have seen King Hamlet's ghost, when it appears again. After hearing from Horatio of the Ghost's appearance, Hamlet resolves to see the Ghost himself.
That night, the Ghost appears again. He tells Hamlet that he is the spirit of his father and discloses that Claudius murdered King Hamlet by pouring poison in his ears. The Ghost demands that Hamlet avenge him; Hamlet agrees and decides to fake madness to avert suspicion. He is, however, uncertain of the Ghost's reliability.
Busy with affairs of state, Claudius and Gertrude try to avert an invasion by Prince Fortinbras of Norway. Perturbed by Hamlet's continuing deep mourning for his father and his increasingly erratic behaviour, they send two student friends of his—Rosencrantz and Guildenstern—to discover the cause of Hamlet's changed behaviour. Hamlet greets his friends warmly but quickly discerns that they have been sent to spy on him.
Polonius is Claudius's trusted chief counsellor; his son, Laertes, is returning to France, and his daughter, Ophelia, is courted by Hamlet. Neither Polonius nor Laertes thinks Hamlet is serious about Ophelia, and they both warn her off. Shortly afterwards, Ophelia is alarmed by Hamlet's strange behaviour and reports to her father that Hamlet rushed into her room but stared at her and said nothing. Polonius assumes that the "ecstasy of love" is responsible for Hamlet's madness, and he informs Claudius and Gertrude. Together, Claudius and Polonius set up Ophelia to spy on him. When she returns his letters and he silently guesses what is going on, he furiously rants at her, and insists she go "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 an agitated running commentary throughout. When the murder scene is presented, Claudius abruptly rises and leaves the room, which Hamlet sees as proof of his uncle's guilt. Claudius, fearing for his life, banishes Hamlet to England on a pretext, closely watched by Rosencrantz and Guildenstern. Alone, Claudius discloses to the audience that he is sending Hamlet to his death.
Gertrude summons Hamlet to her closet to demand an explanation. On his way, Hamlet passes Claudius in prayer but hesitates to kill him, reasoning that death in prayer would send him to heaven. In the bedchamber, an argument erupts between Hamlet and Gertrude. Polonius, who is spying on the whole scene behind an arras, panics when it seems as if Hamlet is about to murder the Queen and cries out for help. Hamlet, believing it is Claudius hiding behind the arras, stabs wildly, killing Polonius.
When he realizes that he has killed Ophelia's father, he is not remorseful, but calls Polonius "Thou wretched, rash, intruding fool". The Ghost appears, urging Hamlet to treat Gertrude gently but reminding him to kill Claudius. Unable to see or hear the Ghost herself, Gertrude takes Hamlet's conversation with it as further evidence of madness. Prior to embarking for England, Hamlet hides Polonius's body, ultimately revealing its location to the King and Gertrude.
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. She comes onstage briefly to give out herbs and flowers. Claudius convinces Laertes that Hamlet is solely responsible; then news arrives that Hamlet is still at large—his ship was attacked by pirates on the way to England, and he has returned to Denmark. Claudius swiftly concocts a plot. He proposes a fencing match between Laertes and Hamlet in which Laertes will fight with a poison-tipped sword, but tacitly plans to offer Hamlet poisoned wine if that fails.
Gertrude interrupts to report that Ophelia has drowned.
We next see two gravediggers discussing while digging her grave, that Ophelia died from apparent suicide. Hamlet arrives with Horatio and banters with a gravedigger, who unearths the skull of a jester from Hamlet's childhood, Yorick. Ophelia's funeral procession approaches, led by Laertes; he leaps into the grave, cursing Hamlet as the cause of her death. Hamlet professes his own love and grief for Ophelia, and he and Laertes grapple, but the brawl is broken up.
Back at Elsinore, Hamlet tells Horatio how he escaped and that Rosencrantz and Guildenstern have been sent to their deaths. A courtier, Osric, interrupts to invite Hamlet to fence with Laertes. With Fortinbras's army closing on Elsinore, the match begins. Laertes pierces Hamlet with a poisoned blade; in the ensuing scuffle, Hamlet takes the sword and fatally wounds Laertes. Gertrude drinks the poisoned wine and dies. In his dying moments, Laertes is reconciled with Hamlet and reveals Claudius's murderous plot. In his own last moments, Hamlet manages to kill Claudius and names Fortinbras as his heir. When Fortinbras arrives, Horatio recounts the tale and Fortinbras orders Hamlet's body borne off in honour.