Hamlet says to Ophelia, "Get thee to a nunnery" so that she will stop enabling people, like her father, Polonius, to spy on Hamlet and undermine him:
Get thee to a nunnery! Why wouldst thou be a breeder of sinners? ... Go thy ways to a nunnery. Where's your father?... If thou dost marry, I'll give thee this plague for thy dowry: be thou as chaste as ice, as pure as snow, thou shalt not escape calumny. Get thee to a nunnery. Go, farewell. Or if thou wilt needs marry, marry a fool; for wise men know well enough what monsters you make of them. To a nunnery, go; and quickly too. Farewell.
And Hamlet has also had it with women, in general, and his mother, Queen Gertrude, specifically. Nuns have none of the traits he so reviles in other individuals of their gender:
I have heard of your paintings too, well enough. God hath given you one face, and you make yourselves another. You jig, you amble, and you lisp; and nickname God's creatures and make your wantonness your ignorance. Go to, I'll no more on't! it hath made me mad. I say, we will have no more marriages. Those that are married already—all but one—shall live; the rest shall keep as they are. To a nunnery, go.
Hamlet wants honesty and loving kindness from women, yet gets none of it from the women in his life. Better for them and him that they make their way to a nunnery.
There is also the irony inherent in the statement. Euphemistically Hamlet is identifying Ophelia as a whore.
Ha Ha. One of my favorite parts in Hamlet! I believe that here Hamlet is displaying his sincere love for Ophelia and wants to protect her from "destruction." Maybe I'm a bit old-fashioned in my views. Anyway, hope it helps to see the line in somewhat a different light.