The key to your question is Horatio. Unless Hamlet persuades Horatio not to kill himself along with the rest, there will be no reliable witness to testify to Hamlet's good reputation and decency. Horatio protests he is more an 'antique Roman' (ie, a Stoic for whom suicide would have been noble rather than damnable) than a Dane, and reaches for the cup of poison (5,ii, 320) as Hamlet lies dying. Hamlet persuades him into the role of historian instead: 'Absent thee from felicity awhile' (ie, death) 'And in this harsh world draw thy breath in pain/To tell my story.' (5,ii,326-328).
Horatio is already established as the play's 'witness'. Hamlet has constantly relied on Horatio for his level head. He made him his confidant to his conversations with the Ghost, his intention to 'put an antic disposition on' (I,v,172) and trust him with this mad disguise, and his plans to trap Claudius. The whole of Hamlet's speech (3,ii,46-75) shortly before the court performance of 'The Murder of Gonzago' is highly significant: not only does it tell us how Hamlet intends to proceed, but how he regards Horatio in relation to the 'truth': 'give me that man/That is not passion's slave...' (61-4). At the end, Horatio accepts the role of witness, the teller of the true story: 'Let me speak to the yet unknowing world/How these things came about' (5,ii, 358-9).