Shakespeare wanted to be sure that his audience fully understood that Hamlet never had any intention of harming his mother. The ghost of his father tells him not to injure her, and he later vows to himself that he will not do so.
Soft! now to my mother!
O heart, lose not thy nature; let not ever
The soul of Nero enter this firm bosom.
Let me be cruel, not unnatural;
I will speak daggers to her, but use none.
My tongue and soul in this be hypocrites
How in my words soever she be shent,
To give them seals never, my soul, consent! (III.ii)
However, when he goes into his mother's chamber, Gertrude gets the idea that he intends to kill her. The reason she gets this idea is that she takes the following words literally.
Gertrude already believes her son is insane. He is wearing a rapier. She thinks he is not speaking metaphorically but actually intends to cut her open with his sharp weapon and force her to look at her own intestines in a mirror. This is why she reacts as follows.
What wilt thou do? Thou wilt not murder me?
Help, help, ho!
Polonius is hiding behind a wall hanging and can't see what is going on. He assumes from what Gertrude has just said that her son is trying to murder her, and so Polonius begins calling for help too.
Hamlet is surprised and frightened, since it has been thoroughly established that he has no intention of harming his mother. He can't imagine why she has started calling for help. He thinks he has walked into a trap. Gertrude mistakenly believes he plans to kill her, and he mistakenly believes that she was an accomplice in her husband's murder. Furthermore, because his mother is calling for the guards in front of him, while a man whom he takes to be King Claudius, is calling for the guards behind him, he thinks he has walked into a trap. He thinks Gertrude and Claudius have conspired to have him thrown into a dungeon and probably killed. This is why he draws his and stabs Polonius to death, thinking he is at last killing Claudius. It is noteworthy that he still has no apparent intention of killing his mother. She stops screaming for help because she is so astonished when her son kills Polonius.
The death of Polonius is the turning point in the play. It causes the deaths of Ophelia and Laertes. Laertes eventually kills Hamlet to avenge his father's and sister's deaths. Claudius sends Hamlet to England with a letter intended to get him beheaded when he arrives. But Hamlet gets Rosencrantz and Guildenstern killed instead. Finally Hamlet kills Claudius with the poisoned foil with which Laertes had intended to kill him. And Gertrude drinks the poisoned wine which Claudius had prepared for Hamlet.
It is noteworthy that when Gertrude tells Claudius about the death of Polonius, she says nothing about ever thinking Hamlet wanted to kill her.
Mad as the sea and wind when both contend
Which is the mightier. In his lawless fit,
Behind the arras hearing something stir,
Whips out his rapier, cries 'A rat, a rat!'
And in this brainish apprehension kills
The unseen good old man. (IV.i)
As far as the question of Gertrude's punishment is concerned, the entire scene in which Hamlet metaphorically sets her up a mirror and makes her look at her own soul is the beginning of her punishment. She suffers nothing but punishment for the rest of the play and ends up dead, possibly having committed suicide by drinking a cup of wine she either knew or suspected to be poisoned. During her stormy interview with her son, she says:
O Hamlet, speak no more!
Thou turn'st mine eyes into my very soul,
And there I see such black and grained spots
As will not leave their tinct. (III.iv)
She was not an accomplice in her first husband's murder. She is only being punished for marrying Claudius and thereby committing "incest" and "adultery." In marrying Claudius she might have also helped him to uaurp the throne by making his candidacy look more legitimate, and thereby helped him to prevent her son from obtaining what should have been his by right of succession. In other words, if Claudius had not married Gertrude he might not have been able to get elected king.
It is interesting that the King's Ghost does not want Hamlet to seek revenge on Gertrude. After divulging the true events of his murder in scene five of the first act, the Ghost says, "But, howsoever thou pursuest this act,/Taint not thy mind, nor let thy soul contrive/ Against thy mother aught. Leave her to heaven,/ And to those thorns that in her bosom lodge/ To prick and sting her"(I.v.89-93). In summary, Hamlet's father doesn't want Gertrude to die, but to live her life with the guilt of being a part of the conspiracy. Sometimes, having to live with oneself is more punishment than death. Simply killing someone in an act of revenge isn't as satisfying as permitting someone to live with their guilt. Also, this shows that Hamlet's father doesn't make his son feel like he must kill his own mother.