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, and took the throne for himself. Denmark has a long-standing feud with neighbouring Norway, in which King Hamlet slew King Fortinbras of Norway in a battle some years ago. Although Denmark defeated Norway, and the Norwegian throne fell to King Fortinbras's infirm brother, Denmark fears that an invasion led by the dead Norwegian king's son, Prince Fortinbras, is imminent.
On a cold night on the ramparts of Elsinore, the Danish royal castle, the sentries Bernardo and Marcellus discuss a ghost resembling the late King Hamlet which they have recently seen, and bring Prince Hamlet's friend Horatio as a witness. After the ghost appears again, the three vow to tell Prince Hamlet what they have witnessed.
As the court gathers the next day, while King Claudius and Queen Gertrude discuss affairs of state with their elderly adviser Polonius, Hamlet looks on glumly. During the court, Claudius grants permission for Polonius's son Laertes to return to school in France, and sends envoys to inform the King of Norway about Fortinbras. Claudius also scolds Hamlet for continuing to grieve over his father, and forbids him from returning to his schooling in Wittenberg. After the court exits, Hamlet despairs of his father's death and his mother's hasty remarriage. Learning of the ghost from Horatio, Hamlet resolves to see it himself.
As Polonius's son Laertes prepares to depart for a visit to France, Polonius gives him contradictory advice that culminates in the ironic maxim "to thine own self be true." Polonius's daughter, Ophelia, admits her interest in Hamlet, but Laertes warns her against seeking the prince's attention, and Polonius orders her to reject his advances. That night on the rampart, the ghost appears to Hamlet, telling the prince that he was murdered by Claudius and demanding that Hamlet avenge him. Hamlet agrees and the ghost vanishes. The prince confides to Horatio and the sentries that from now on he plans to "put an antic disposition on", or act as though he has gone mad, and forces them to swear to keep his plans for revenge secret. Privately, however, he remains uncertain of the ghost's reliability.
Soon thereafter, Ophelia rushes to her father, telling him that Hamlet arrived at her door the prior night half-undressed and behaving erratically. Polonius blames love for Hamlet's madness and resolves to inform Claudius and Gertrude. As he enters to do so, the king and queen finish welcoming Rosencrantz and Guildenstern, two student acquaintances of Hamlet, to Elsinore. The royal couple has requested that the students investigate the cause of Hamlet's mood and behaviour. Additional news requires that Polonius wait to be heard: messengers from Norway inform Claudius that the King of Norway has rebuked Prince Fortinbras for attempting to re-fight his father's battles. The forces that Fortinbras had conscripted to march against Denmark will instead be sent against Poland, though they will pass through Danish territory to get there.
Polonius tells Claudius and Gertrude his theory regarding Hamlet's behaviour, and speaks to Hamlet in a hall of the castle to try to uncover more information. Hamlet feigns madness but subtly insults Polonius all the while. When Rosencrantz and Guildenstern arrive, Hamlet greets his "friends" warmly, but quickly discerns that they are spies. Hamlet becomes bitter, admitting that he is upset at his situation but refusing to give the true reason why, instead commenting on "what a piece of work" humanity is. Rosencrantz and Guildenstern tell Hamlet that they have brought along a troupe of actors that they met while traveling to Elsinore. Hamlet, after welcoming the actors and dismissing his friends-turned-spies, asks them to deliver a soliloquy about the death of King Priam and Queen Hecuba at the climax of the Trojan War. Impressed by their delivery of the speech, he plots to stage The Murder of Gonzago, a play featuring a death in the style of his father's murder, and to determine the truth of the ghost's story, as well as Claudius's guilt or innocence, by studying Claudius's reaction.
Polonius forces Ophelia to return Hamlet's love letters and tokens of affection to the prince while he and Claudius watch from afar to evaluate Hamlet's reaction. Hamlet is walking alone in the hall as the King and Polonius await Ophelia's entrance, musing whether "to be or not to be". When Ophelia enters and tries to return Hamlet's things, Hamlet accuses her of immodesty and cries "get thee to a nunnery", though it is unclear whether this, too, is a show of madness or genuine distress. His reaction convinces Claudius that Hamlet is not mad for love. Shortly thereafter, the court assembles to watch the play Hamlet has commissioned. After seeing the Player King murdered by his rival pouring poison in his ear, Claudius abruptly rises and runs from the room: for Hamlet, proof positive of his uncle's guilt.
Gertrude summons Hamlet to her room to demand an explanation. Meanwhile, Claudius talks to himself about the impossibility of repenting, since he still has possession of his ill-gotten goods: his brother's crown and wife. He sinks to his knees. Hamlet, on his way to visit his mother, sneaks up behind him, but does not kill him, reasoning that killing Claudius while he is praying will send him straight to heaven while his father's ghost is stuck in purgatory. In the queen's bedchamber, Hamlet and Gertrude fight bitterly. Polonius, spying on the conversation from behind a tapestry, makes a noise.
Hamlet, believing it is Claudius, stabs wildly, killing Polonius, but pulls aside the curtain and sees his mistake. In a rage, Hamlet brutally insults his mother for her apparent ignorance of Claudius's villainy, but the ghost enters and reprimands Hamlet for his inaction and harsh words. Unable to see or hear the ghost herself, Gertrude takes Hamlet's conversation with it as further evidence of madness. After begging the queen to stop sleeping with Claudius, Hamlet leaves, dragging Polonius's corpse away.
Hamlet jokes with Claudius about where he has hidden Polonius's body, and the king, fearing for his life, sends Rosencrantz and Guildenstern to accompany Hamlet to England with a sealed letter to the English king requesting that Hamlet be executed immediately.
Demented by grief at Polonius's death, Ophelia wanders Elsinore. Laertes arrives back from France, enraged by his father's death and his sister's madness. Claudius convinces Laertes that Hamlet is solely responsible, but a letter soon arrives indicating that Hamlet has returned to Denmark, foiling Claudius' plan. Claudius switches tactics, proposing a fencing match between Laertes and Hamlet to settle their differences. Laertes will be given a poison-tipped foil, and Claudius will offer Hamlet poisoned wine as a congratulation if that fails. Gertrude interrupts to report that Ophelia has drowned, though it is unclear whether it was suicide or an accident exacerbated by her madness.
Horatio has received a letter from Hamlet, explaining that the prince escaped by negotiating with pirates who attempted to attack his England-bound ship, and the friends reunite offstage. Two gravediggers discuss Ophelia's apparent suicide while digging her grave. Hamlet arrives with Horatio and banters with one of the gravediggers, who unearths the skull of a jester from Hamlet's childhood, Yorick. Hamlet picks up the skull, saying "alas, poor Yorick" as he contemplates mortality. Ophelia's funeral procession approaches, led by Laertes. Hamlet and Horatio initially hide, but when Hamlet realizes that Ophelia is the one being buried, he reveals himself, proclaiming his love for her. Laertes and Hamlet fight by Ophelia's graveside, but the brawl is broken up.
Back at Elsinore, Hamlet explains to Horatio that he had discovered Claudius's letter with Rosencrantz and Guildenstern's belongings and replaced it with a forged copy indicating that his former friends should be killed instead. A foppish courtier, Osric, interrupts the conversation to deliver the fencing challenge to Hamlet. Hamlet, despite Horatio's pleas, accepts it. Hamlet does well at first, leading the match by two hits to none, and Gertrude raises a toast to him using the poisoned glass of wine Claudius had set aside for Hamlet. Claudius tries to stop her, but is too late: she drinks, and Laertes realizes the plot will be revealed. Laertes slashes Hamlet with his poisoned blade. In the ensuing scuffle, they switch weapons and Hamlet wounds Laertes with his own poisoned sword. Gertrude collapses and, claiming she has been poisoned, dies. In his dying moments, Laertes reconciles with Hamlet and reveals Claudius's plan. Hamlet rushes at Claudius and kills him. As the poison takes effect, Hamlet, hearing that Fortinbras is marching through the area, names the Norwegian prince as his successor. Horatio, distraught at the thought of being the last survivor and living whilst Hamlet does not, says he will commit suicide by drinking the dregs of Gertrude's poisoned wine, but Hamlet begs him to live on and tell his story. Hamlet dies in Horatio's arms, proclaiming "the rest is silence". Fortinbras, who was ostensibly marching towards Poland with his army, arrives at the palace, along with an English ambassador bringing news of Rosencrantz and Guildenstern's deaths. Horatio promises to recount the full story of what happened, and Fortinbras, seeing the entire Danish royal family dead, takes the crown for himself, and orders a military funeral to honour Hamlet.