Shakespeare has Gertrude enter in Act 4, scene 7 to announce the death of Ophelia. Why would Shakespeare have her enter just when she does?
There are several reasons for Gertrude's appearance. The first is to change the tempo of the play which has been focused on the plot to kill Hamlet. The characters, especially Laertes, are angry and and have just been plotting a bloody ending to Hamlet's life. Gertrude's appearance suddenly turns the mood on stage from anger and hostility to sadness and solemnity. In addition, it gives Laertes even more motivation to kill Hamlet because he knows how much Hamlet meant to his sister. Lastly, it seems to be the last straw in Gertrude's true affection for Claudius. At the end of the scene, Claudius tells his wife to follow him, complaining how much he had to do to calm Laertes. For the first time, Gertrude does not follow and seems to be grasping the full consequences of her marriage to Claudius. Her son is considered mad, Polonius is dead, Ophelia, whom she had hoped would marry Hamlet, is dead, Laertes is furious and Gertrude doesn't know when the next tragedy is going to occur.