Ophelia is, at the outset of the play's action, a beautiful girl whose father is very concerned with gaining and consolidating his power in the court of Denmark. So when Polonius learns of Ophelia and Hamlet's love affair, he forbids her to pursue it and she, being the obedient daughter, breaks it off. Hamlet is confused by this, along with his greater confusion about his father's death and all that goes with it, and Ophelia is hurt and trying to make sense of it for him while also being loyal to her father.
When Hamlet loses it (or appears to), then kills her father, Ophelia's sanity is lost and she raves and cries and eventually kills herself. Shakespeare presents all of these phases in the way that she speaks and interacts with the other characters (obviously) but spends a great deal of time developing her character as one of the innocent victims of all the intrigue in the court between Hamlet, Claudius, Hamlet Sr., Gertrude, et al.