In Shakespeare’s play, Hamlet's death comes almost at the end of Act 5, Scene 2. In this scene Hamlet tells Horatio that Rosencrantz and Guildenstern have died offstage: they were executed in his stead by the king of England. A courtier named Osric jokes with Hamlet and Horatio, making many puns (plays on words that are often humorous). Osric also tells Hamlet about Claudius's proposed duel; Horatio attempts to dissuade Hamlet, but Hamlet accepts the challenge.
Hamlet apologizes to Laertes for murdering Polonius and asks forgiveness; Laertes declines. Claudius states that whoever lands two hits on the other first can drink from a cup of wine that Claudius has actually poisoned; Hamlet lands two hits but refuses to drink. Instead, Gertrude drinks to her son. Laertes starts to doubt the righteousness of his revenge, but then cuts Hamlet with his poisoned blade. Hamlet manages to then injure Laetres with Laertes' own poisoned weapon.
Hamlet's own death comes hundreds of lines after he is mortally wounded by Laertes. He knows he is dying. Hamlet simultaneously tries to process his own death and deal with his responsibilities as the Prince of Denmark. It's heart-wrenching. Having Hamlet's death so drawn out is fitting for the philosophical prince. It creates the challenge for Shakespeare of ensuring that not a single audience member thinks his tragic hero should just die already, and of crafting a thoughtful conclusion that takes into account all Hamlet's previous grappling with the enigma of death. Hamlet's last words are 'The rest is silence,' which is the perfect ending of the play.