At the time the play was written, Denmark had an elected monarchy. This meant that the king would be elected by the inner circle of noblemen and/or people of the royal court -- a noble inner circle. There is nothing in the text that explains why Claudius is elected as the next king rather than Hamlet, but a reader can have a few, logical speculations. The first is that, as you noted, Hamlet was away at school when he his father died. There might have been a sentiment that young Hamlet was too removed from court life and the "ins and outs" of the day to day rule of Denmark. He was young, away at school still, and untested in his leadership abilities. At the same time, Claudius has likely lived his whole life at Elsinore as the right hand man to his brother, King Hamlet. He would be able to more seamlessly take over control and keep Denmark running smoothly. There would hardly be any change in leadership. This is especially true in light of the fact that he also married his former sister-in-law, and so the queen remained the same through the transition. Claudius would have had many loyal friends and perhaps even people who owed him favors or curried his favor and therefore voted for his taking the throne. Young Hamlet is still young, and he his time would come eventually, perhaps when he is more capable and experienced. Claudius, in his first speech to the court thanks them and remarks that he and Gertrude have "herein barred / Your better wisdoms, which have freely gone / With this affair along." In this next conversation with Hamlet, he assures him that he is "the most immediate to the throne" meaning that Hamlet will likely be elected next, but it is a kind of cold comfort when you consider that Claudius could live many more years and it might be decades before Hamlet could take the throne.
At the end of the play, once Hamlet knows that Claudius is dead and that he himself has received a mortal wound, Hamlet tells Horatio that he "sets his election lights on Fortinbras." This is another reference to the elected monarchy and indicates that Hamlet is expressing his preference for who should take over the kingship of Denmark upon his death. Hamlet wants Denmark to pass, as peacefully as possible, into capable leadership, and Fortinbras seems to logical choice.