The recently passed away King Hamlet comes to Hamlet in ghost form and tell Hamlet that Claudius was the one who killed him. He asks Hamlet to get his revenge on Claudius and avenge his death.
Hamlet wants to get more proof that the ghost was really telling the truth about Claudius killing King Hamlet. He decides to present a play with a similar story to what the ghost told Hamlet in front of Claudius. Hamlet believes that if Claudius looks uncomfortable during the play, he killed King Hamlet.
Hamlet is prepared to kill Claudius but decides not to once he realizes that Claudius is praying. If he kills Claudius during prayer, he will go straight to heaven so instead he decides to wait for a time when Claudius is sinning to kill him.