When Horatio and Hamlet see the funeral procession coming to the graveyard where Hamlet has just ruminated on Yorick's skull, the two men hide. However, at the point that Hamlet sees Laertes jump into Ophelia's grave, declaring his love for his sister, he rushes out of hiding. He too jumps into the grave. He fights with Laertes and says he loves Ophelia more than 40,000 brother could:
forty thousand brothers
Could not, with all their quantity of love
make up my sum
He also says he loves Ophelia so much he would be willing to be buried alive with her.
All of this shows Hamlet to be "passion's slave" at this moment. It is not wise or logical for him to reveal himself to Laertes, since Laertes is bent on killing him to avenge the death of his father. Laertes also holds Hamlet responsible for Ophelia's suicide. Nevertheless, Hamlet is so overcome with grief that he cannot control himself.