In the play, Ophelia acts as a sort of observer and reporter as she is one of the only people that Hamlet opens up to, and one of the only people that he even partially trusts. Unfortunately she is also manipulated by her father Polonius as he tries to help Claudius cement his hold on the throne. Polonius tells her that her relationship with Hamlet cannot possibly be real nor can she hope to continue it. Then, when it is convenient to send her out to try and find out what Hamlet is thinking, he does so.
This is the beginning of the end for Ophelia as Hamlet flies into a rage and weaves back and forth between expressing affection for her and telling her to get "to a nunnery." After this spectacle and her father's death she descends into madness.
Her death comes almost as a relief to her spasmodic behavior but it also serves to further strengthen Hamlet's resolve to do something about Claudius and the suspicions he has. Her burial serves as the occasion for Hamlet to express his powerful love for her, one of the few clear and honest expressions of love he makes in the play, and to set up the conflict between he and Laertes that will end in his death.