The answer to your question hinges upon the literary definition of tragedy. Hamlet, along with any other tragic hero, has a tragic flaw that brings about his demise. In my opinion, Hamlet's tragic flaw is inaction; however, that has always been controversial. Quite simply, Hamlet is a famous Shakespearean tragedy and, therefore, must end this way. In fact, eNotes definition of tragedy from the Guide to Literary Terms is "a serious play in which the chief figures, by some peculiarity of character, pass through a series of misfortunes leading to the final catastrophe." By definition, then, the tragedy of Hamlet must end in this final catastrophe: the death of all principal characters. It is the manner in which this final catastrophe comes about that exhibits Shakespeare's genius.
Being the tragic hero, Hamlet has a tragic flaw that must lead to his downfall. His indecisiveness coupled with hasty actions at the end lead to the conclusion. The ending of Hamlet appropriately closes all the conflicts, as well as reinforcing Hamlet's decision that since death is unavoidable, he would rather die fighting for his father's revenge than sitting idly by. All the characters that die in the end are in some way responsible for harm to others, which also makes the ending fitting.
Hamlet has to end in this way because it is a tragedy. Under the conventions of the day a tragedy will end in deaths of the major protagonists and a comedy will marvellously end in a reconciliation or marriage. After his soul searching and quest to find if the ghost is telling the truth Hamlet must confront Claudius and for the tragic inevitability to occur that must lead also to the death of the 'hero.' Hamlet is left stripped of his mother, father and girlfriend, all are dead. He has murdered Polonius and seen to the killing of his two old school friends, the die is cast for the end outcome; Hamlets revenge on Claudius and his own death.