At the half way point through the novel, Hamlet has been beset by bad news from the ghost and betrayal by his friends. He is extremely frustrated by his mother's quick marriage and his own inability to take the action he needs to against Claudius. In the nunnery scene, Hamlet confronts Ophelia and gives her a very stern talk about what he sees in her as a woman. It is a devastating speech for Ophelia who is shocked and hurt by Hamlet's attitude.
Hamlet tells Ophelia that he loved her once, and then contradicts himself and tells her he never loved her. On the heels of that hurtful comment he instructs her to get herself to a nunnery. It would seem that he is referring to a house for nuns -- a religious order of nuns who would take a vow of celibacy. He tells her to do this so that she will not become "a breeder of sinners." If she shuts herself away from men, then she can't produce children who become sinners like him and like everyone else. He is especially condemning of men, calling them "errant knaves all." It is important to note here that in Shakespeare's day, a nunnery could also be used as a slang term for a house of prostitution. If he intends this meaning, he is being even more cruel to Ophelia. In the next section of the conversation he does insult women for their flirtatious ways ("You jig, you amble, and you lisp; you nickname God's creatures and make your wantonness your ignorance.") He tells Ophelia that women make fools of men and that he hopes if she does marry she be "as chaste as ice" meaning sexually frigid. No matter how he intends the meaning of the word nunnery, it is a cruel awakening to Ophelia of how much she perceives that Hamlet has changed.
Unfortunately for her, she doesn't realize that Hamlet is only acting crazy and this speech is for the benefit of the King and Polonius whom he suspects are spying on the whole exchange. When he asks her where he father is and she replies, "at home," He immediately thinks she is lying. His next line is "let the doors he shut upon him, that he may play the fool nowhere but in's own house." Hamlet is essentially talking to the spying men and letting them know that he is on to them with this insult.
Once Hamlet leaves the stage, Ophelia is left alone on stage. In the soliloquy that follows, she shares her deep sadness for "what a noble mind is here o'erthrown." She seems to be more upset for how changed Hamlet is, in that he would speak to her this way, than for what he actually said to her. It shows a loyalty to her character that makes the reader feel sorry for the way Hamlet treated her. He may have thought he had no choice but to keep up the crazy act in this way, but he was awfully cruel in the process.