Why does Hamlet kill Polonius through the arras even though he passed Claudius praying?
When Hamlet is summoned by his mother he doesn't know whether she had any involvement in his father's murder. She might have been an accessory before the fact or after the fact or completely ignorant of what Claudius was going to do or what he had done after it was done. She has good cause to think her son is insane. He knows he is not insane. He has also assured himself (and the audience) that he intends no harm to her. In Act 3, Scene 1, he tells himself:
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 somever she be shent,
To give them seals--never, my soul, consent.
The audience knows that Hamlet has no intention to harm his mother. However, she gets the wrong idea when he becomes angry and says:
Come, come, and sit you down. You shall not budge.
You go not till I set you up a glass
Where you may see the inmost part of you.
Gertrude takes these figurative words literally. He has a sword. He has just shown it when he encountered Claudius praying. She thinks her mad son is literally planning to set a mirror up in front of her, then slice her open so that she can see her own internal organs. She is terrified and calls:
What wilt thou do? thou wilt not murder me?
She is calling for the guards. Hamlet doesn't understand what is happening. Since his intentions are totally innocent, he thinks he has walked into a trap. If so, the guards will imprison him, and he could remain a prisoner indefinitely or even killed. Then Polonius confuses him further by calling from concealment behind him:
What, ho! Help, help, help!
Now Hamlet is sure he is being trapped. Impulsively, he draws his sword and thrusts it through the arras, killing Polonius. Hamlet doesn't have to think he is killing Claudius. As far as he knows, it could be anybody hiding there, such as a guard who is part of the scheme to entrap and imprison him. Hamlet knows that if the guards apprehend him and take him away, his mother will always believe that he is insane and intended to kill her in a horrible manner, by cutting her open and making her look at her gaping wound in a mirror. In his ensuing conversation with his mother, he realizes that she knows nothing about his father's murder, and she realizes that her son is not insane and did not intend her any harm.
Shakespeare wanted Polonius to be killed by Hamlet for the purposes of his plot. Most of the events that subsequently occur, such as Ophelia's madness and apparent suicide, evolve from the killing of Polonius. But Polonius also has to die so that he won't overhear what Hamlet tells his mother or overhear Hamlet talking to his father's ghost. If Polonius overheard everything, he would report to Claudius that Hamlet thought Claudius had killed King Hamlet and that young Hamlet thought he was talking to his father's ghost. Polonius would think these were lunatic ravings, but Claudius would believe that Hamlet really had been talking to his father and that Hamlet really knew about Claudius' murdering the legitimate king in the garden.
In this scene we see Hamlet's first impulsive action, the killing of Polonius. Why, after passing by Claudius, does Hamlet kill Polonius? The answer is that he does not know who is behind the arras: he does not know he is killing Polonius.He is striking out at a hidden person, a spy, who has started to shout for the palace guards. Hamlet reacts instinctively, without thought. His blood is already boiling because of his enraged confrontation with Gertrude. She has started to call for the palace guards herself. Another voice raises the alarm at the same time. He doesn't think; he reacts to an alarm, an alarm that threatens him. Afterward, he says he is punish for not avenging his father by killing Polonius by mistake, while Polonius is punished for his treachery by being killed by Hamlet.
After he skips out on this opportunity to kill Claudius because he thinks Claudius is praying, Hamlet is scolding himself for not taking action. Remember that we found out that Claudius was not praying, so it in fact would have been a good time to carry out his vengeance plot, but Hamlet missed the opportunity by looking for his own revenge instead administering the Ghost's revenge: Hamlet himself didn't want to give Claudius the chance to be forgiven and go to heaven.
I don't think these scenes are to be seen as unrelated. I see it more as Hamlet is/has always been more about confronting his mother than obeying his father. In Hamlet's mind, Claudius might be back there after receiving forgiveness. This would send Claudius to heaven as well.
He instead cannot keep from possibly killing her lover in front of her eyes, regardless of giving Claudius his heavenly reward. It is a rash and bloody deed. This prompts the visitation to whet his almost blunted purpose.... he wasn't just going about this in the wrong way, his wrong direction is hitting a dead end.
Remember, too, that Hamlet pledges to be more active. His first act after that pledge is to speak with his mother and then to kill whom he thinks is Claudius behind the tapestry hanging on her wall. It is conceivable that Claudius would have time to get to Gertrude's chambers since in many castles there were secret passages from the King's chambers to his Queen's, or his mistress's or both.
Certainly, Hamlet is totally repulsed by Polonius, who is duplicitous and hypocritical. Perhaps he thinks that Claudius is behind the arras and he takes his second chance to be rid of him rather than let it go as he has done the first. When Hamlet sees that he has killed Polonius there is no regret because the fatuous man is repugnant to Hamlet.
He does not know who is behind that and believed it was a spy. He did not want others to hear about what was happening in the room and thus killed the person. But in truth, he had a feeling it could be Claudius and that is why he went straight ahead to kill him and rid him once and for all. At that time he is furious after talking with his mom and with anger/being alert he kills whoever is beyond the arras with no regrets.
Hamlet did not kill Claudius for the simple reason Claudius was praying before he died (Hamlet did not want Claudius to be praying whilst he died, which would guarantee him a place in Heaven - whereas Hamlet clearly believes Claudius should go to Hell.
He kills Polonius seemingly by mistake, as as soon as the Queen exclaims - "O, what a rash and bloody deed!", he asks - "is it the King?". He was not aware it was Polonius behind the arras, and was hoping it to be the King - however, he clearly shows no remorse for murdering Polonius - "what a rash...intruding fool."