Madness is a consistent theme in William Shakespeare's Hamlet, and most of it centers around the protagonist, Hamlet. It is true that the appearance of the Ghost makes Hamlet and others question reality (and their own sanity for believing the Ghost exists), and Ophelia suffers from some form of madness after her father's murder and Hamlet's apparent betrayal. The majority of the madness, however, concerns Hamlet.
After he meets with his father's ghost, Hamlet resolves to avenge King Hamlet's murder by killing Claudius. Though he does not reveal a specific plan, he asks his closest friends a favor:
Here, as before, never, so help you mercy,
How strange or odd soe'er I bear myself,
As I perchance hereafter shall think meet
To put an antic disposition on,
That you, at such times seeing me, never shall,
With arms encumber'd thus, or this headshake,
Or by pronouncing of some doubtful phrase,
As 'Well, well, we know,' or 'We could, an if we would,'
Or 'If we list to speak,' or 'There be, an if they might,'
Or such ambiguous giving out, to note
That you know aught of me....
It is clear that part of Hamlet's plan involves putting on an "antic disposition," some appearance of madness which will be noticeable enough that they will look at him and their body language will reveal that what he is doing is an act.
We know that Hamlet is an actor: he quotes a long speech from one play, writes some lines for another, and even gives the actors advice (do not overact) before they take the stage. This acting experience serves him well as he begins his plan to feign madness.
After Hamlet makes his resolve, he does many things which cause people to believe he is mad; even the gravediggers have heard the rumors. He acts particularly mad around Polonius and Ophelia, and they are both confused by his behavior. Gertrude believes her son is just upset about his father's death and her "o'erhasty" marriage to Claudius; Claudius suspects that Hamlet knows the truth about King Hamlet's death. Neither Gertrude nor Claudius is willing to act until they have more information, which allows Hamlet to continue his plan of feigned madness.
Hamlet does reveal what seems to be the truth to his double-dealing friends, Rosencrantz and Guildenstern. He boldly asserts that
I am but mad north-north-west.
When the wind is southerly, I know a hawk from a handsaw.
Hamlet undoubtedly knows he can reveal the truth to them because they are too simple-minded (or perhaps single-minded) to understand what he is saying. This proves to be true, because they do not tell Claudius what Hamlet said; if they had thought it was significant, they surely would have sold out their former friend.
Hamlet also reveals his true state of mind, as well as his plan, to his mother. After he scolds her for marrying such a poor substitute for King Hamlet, he tells Gertrude:
In the next scene, she has the chance to tell Claudius what Hamlet told her. Instead she tells her husband that Hamlet is
Mad as the sea and wind, when both contend
Either she believes her son and has chosen not to tell Claudius the truth, or she believes he is mad and has chosen to disregard Hamlet's claim.
Hamlet clearly begins the play by acting mad; the primary question of madness in this play is whether Hamlet suffers from real madness at the end. Examine Hamlet's final acts to decide whether you believe he is mad.
I am not an adherent to madness as a theme. I see it more as a theatrical device. We have no way establishing whether Hamlet suffers from madness or not. Clearly Shakespeare left the question to the actors. How much is real and how much is for show. It fits better within the image/reality theme. The play is marvelously ambiguous on this and other points. The sort of things that fill out high school essays. But the lack of an answer is a testament to the idea that the journey is worth more than the destination. Now Ophelia, being separated from herself and her fair judgment is another matter entirely. I have posted on that issue elsewhere.