Answer:
In what they call the Nunnery Scene, in Hamlet Act 3 Scene 1, Hamlet gets angry at Ophelia. The scene is very complicated and it is difficult to find a consistent explanation of the lines.
In one theory, Hamlet becomes mad at Ophelia because he thinks she's become Claudius's courtesan. The situation is that Hamlet knows he was summoned there by Claudius. Claudius, himself, tells us that he summoned Hamlet.
Hamlet finds Ophelia there, and then she returns the gifts he gave her, so Hamlet thinks Ophelia is returning his gifts because Claudius told her to. That's how it looks to him. Hamlet thinks he was summoned there by Claudius so Ophelia could return his gifts.
Thus, Hamlet suspects that Ophelia must have gone over to Claudius. Hamlet thinks the same kind of thing has happened again that he's already seen, that being, first his mother went to Claudius and married him, then Hamlet's old friends R & G went to Claudius and started working for him, so now Hamlet thinks it's happened with Ophelia, too, when she returns his gifts, after Claudius summoned him there. And why would a lecherous old king be interested in a pretty young girl? Hamlet draws the obvious conclusion. He's gotten the tragically wrong idea that Ophelia is a prostitute.
Here is another possible interpretation of what is going on. Hamlet expects Claudius to be behind the curtains and knows he is there watching this. He bumps into Ophelia. There is a lot he'd like to say to Ophelia, but he has to be careful because he is being overheard. She is going to return love-tokens to him (she is in fact doing this because her father told her to do it) His first thought is to deny it; he doesn't want Claudius to know about Ophelia and him. Then he thinks she is behaving oddly "Are you honest?" and she gets worried. Is he on to her? Hamlet then tries to tell her to get out of his life because he is too dangerous, and retire to a convent. "Why would thou be a breeder of sinners?" Then something happens, and he asks the question, "Where is your father?" Her answer gives away the fact that she knows that Polonius is behind the arras--she knew it all the time. Here he was trying to be nice to her, and she was selling him out. He is furious and instead of telling her to get to a "nunnery" that is a convent he tells her to get to the "nunnery" that is a brothel, because she had sold herself out like a prostitute.
Ophelia certainly is Claudius's and Polonius's tool. She has engaged Hamlet in conversation knowing that the purpose of the thing is to trap him in front of the hidden witnesses. She has sold out to Claudius just like Rosencrantz and Guildenstern and Gertrude have. But she has done so because she is very weak, dependent and somewhat dimwitted, and she is trying to be a dutiful daughter and obey her father, without reflecting on what this might mean for Hamlet, or that it implies choosing sides in some court intrigue. When she finds that out, it will drive her mad. Hamlet is wrong about Ophelia, but he doesn't know that. She hasn't really gone over to Claudius. However, Hamlet's mistaken idea makes him very angry.