One technique that Shakespeare uses exceptionally well in this play is the introduction in Act I of two key questions that are posed and remain unanswered for the duration of the next couple of acts. This is something that engages the audience's interest and also causes them to become involved in forming their own opinion about what they see. The first of these questions is introduced in Act I scene 4, and concerns the nature of the Ghost. Horatio makes it perfectly clear to Hamlet that the Ghost might not actually be Hamlet's dead father, but some kind of spirit who is trying to lead Hamlet to his death:
What if it tempt you toward the flood, my lord,
Or to the dreadful summit of the cliff
That beetles o'er his base into the sea,
And there assume some other horrible form
Which might deprive your sovereignty of reason
And draw you into madness?
This question of the true nature of the Ghost is something that is never reconciled, even after Hamlet has proof that Claudius did kill his father as the Ghost claims. The uncertainty over the precise nature of the Ghost is something that hangs over Hamlet and his actions and engages the attention of the audience as they to are invited to speculate on the Ghost and whether he is genuine or not.
Secondly, another crucial question that is introduced in Act I scene 5 is whether Hamlet is actually mad or not. He makes Horatio and Marcellus swear that they will not tell anyone he plans to put on an "antic disposition," but the audience is never fully sure of whether Hamlet's actions are the actions of a madman or somebody who is pretending to be mad. There is a sense in which Hamlet is a man under immense pressure, and it would only be reasonable to assume that he cracks under that pressure to a certain extent and loses his sanity at various points in the play as a result. However, no clear message is ever given regarding this, and Hamlet's sanity remains one of the key questions that still occupies critics to this day. The introduction of these two questions early on in the play is one way in which Shakespeare engages the interest of the audience and keeps them spellbound by the action.
Another technique that Shakespeare uses to hold attention in Hamlet is one that is common among all good playwrights. Lajos Egri, in his excellent book The Art of Dramatic Writiing, calls this "orchestration." The analogy is to orchestral music and to opera. If Hamlet were an opera, for example, Hamlet would be the tenor, Ophelia the soprano, Gertrude probably the contralto, and Claudius the baritone.
Shakespeare keeps changing the characters and the combinations of characters on stage. You will notice that he shows the characters together in nearly every possible combination of twos and threes. Hamlet appears with the Ghost (who would certainly be the basso profundo), with Ophelia, with Polonius, with his mother in a stirring duet, in a trio with Rosencrantz and Guildenstern, and so on. The scene in which Claudius is praying for forgiveness for his sins would make an excellent solo for that character. There is a trio involving Gertrude, Claudius and Laertes and another involving Polonius, Ophelia and Laertes. There is an opportunity for a love duet between Hamlet and Ophelia. There is an excellent opportunity for a "mad scene" involving the soprano Ophelia. There are, of course, plenty of opportunities for solos (soliloquies) by the tenor Hamlet.
The principle involved in orchestration is to have a number of contrasting characters and to keep changing them around so that the audience never knows what to expect next. This holds audience attention. The way to lose audience attention is to focus on one character and one scene too long. Variety is the spice of life.
Hamlet is possibly the best orchestrated of all of Shakespeare's tragedies, which may account for its popularity. The comedy As You Like It is very well orchestrated with a large cast of contrasting characters. Rosalind would be the soprano and Orlando the tenor. Celia would be the contralto. Touchstone would be like Mozart's Papageno in The Magic Flute.