The dagger seeks to solidify the mental process Macbeth goes through from his meeting with the witches until his murder of Duncan. The soliloquy serves the dramatic purpose both of explaining his thought process and demonstrating his mental instability.
In Act 1, Macbeth has already established his ambition. Duncan tells him how pleased he is with his bravery, and rewards him with the promotion to the title of Thane of Cawdor. Macbeth is not satisfied. The witches have told him he will be king, so he expects Duncan to name him, not the king’s son, as his successor.
[Aside.] The Prince of Cumberland! That is a step(55)
On which I must fall down, or else o'erleap,
For in my way it lies. Stars, hide your fires;
Let not light see my black and deep desires:
The eye wink at the hand; yet let that be
Which the eye fears, when it is done, to see.(60) (Act 1, Scene 5, enotes etext pdf p. 18)
Things get further complicated because Macbeth has written home to his wife and told her what the witches said. LadyMacbeth’s attitude is that if Duncan won’t give Macbeth what he deserves, Macbeth should just take it!
Macbeth, while ambitious, was apparently not overly so until the witches and Lady Macbeth encouraged him. His wife tells him to pretend to be nice to Duncan and then take him out.
To beguile the time,
Look like the time; bear welcome in your eye,
Your hand, your tongue; look like the innocent flower,(70)
But be the serpent under't. (Act 1, Scene 5, p. 20)
Macbeth tells her they’ll talk about it later. She has stirred things up in him, and he cannot decide what to do. He sort of feels like Duncan is his kinsman (they are related), and it would be wrong to kill him.
He's here in double trust:
First, as I am his kinsman and his subject,
Strong both against the deed; then, as his host (Act 1, Scene 7)
It is this guilt and confusion that leads Macbeth to imagine or hallucinate the floating dagger. He has already decided, “I have no spur/To prick the sides of my intent, but only/Vaulting ambition, which o'erleaps itself/And falls on the other” (Act 1, Scene 7). It is here that Lady Macbeth interrupts him. He feels like Duncan has done nothing to deserve murder. Lady Macbeth tells him “screw your courage to the sticking-place” (p. 24) and leaves him to ponder.
By Act 3, Scene 1, Macbeth sees the dagger. He talks directly to it, wondering if it is real why he cannot touch it.
Or art thou but(45)
A dagger of the mind, a false creation,
Proceeding from the heat-oppressed brain? (Act 3, Scene 1)
He basically is asking himself, AM I CRAZY? The audience might be wondering the same thing. Yet, through the device of the dagger, they realize that he might be. Yet by the end of the soliloquy, Macbeth has made up his mind to kill Duncan, and the audience is right there with him. "Macbeth’s conscience creates the vision of the dagger, either to halt his plans by revealing the horror of the act or, as Macbeth believes, to beckon him forward." (enotes Act 2 summary and analysis)