This is actually a tricky question because no one actually named young Hamlet as heir to the throne. He is never actually the King. In the time of the play, Denmark is an elected monarchy. This means that it is not an automatic event that the first born son of the monarch will be king. This is how Claudius gets the throne after King Hamlet's death. While we don't know what exactly happened in the immediate aftermath of King Hamlet's death, it is clear that Claudius made a case to the royal court that he should take the throne. He may have suggested that Prince Hamlet was too young, too inexperienced, still attending school and therefore too removed from the day-to-day leadership of the country. No matter what, Claudius does acknowledge the court's role in putting him on the throne when he thanks them "for going with this affair along."
In Act 5, after Claudius dies, Hamlet is the next logical royal subject to ascend the throne. In all likelihood he would be elected king if he had lived. Hamlet knows that he has been mortally wounded and in a "kingly" act says, "I do prophesy the election lights / On Fortinbras. He has my dying voice." He is ensuring that the throne of Denmark pass to a suitable leader -- a member of royalty -- who also impressed Hamlet with his drive and determination earlier in the story. In these lines you see the reference to the "election lights" reminding the audience that the royal court will have to affirm the next king through an election, but we are to assume that Hamlet's request would naturally be honored.