In Act II Sc.2 Claudius and Gertrude are worried that Hamlet has become insane. They don't know that Hamlet is only pretending to be mad and so Claudius asks Hamlet's two friends Rosencrantz and Guildenstern to cheer him up and find out the reason for his strange behaviour and see whether his parents can help him out of his difficult situation:
"To draw him on to pleasures and to gather, So much as from occasion you may glean, Whether aught, to us unknown, afflicts him thus That, opened, lies within our remedy."
The two friends readily agree to help Claudius and Gertrude and they meet Hamlet and begin to quiz him about his strange behaviour. Hamlet as soon as he meets them realises that they are decoys meant to remove his disguise and expose him, so he becomes very depressed that his two friends have turned traitors and he speaks these lines. Hamlet praises God's creation "Man" in fantastic terms:
"What a piece of work is a man! how noble in reason!
how infinite in faculty! in form and moving how
express and admirable! in action how like an angel!
in apprehension how like a god! the beauty of the
world! the paragon of animals!"
But he concludes by remarking that man is nothing more than"quintessence of dust." That is God created man from dust and after he dies will become nothing but dust. Man for all his physical beauty and intellectual achievements is nothing but dust.
Hamlet thus indirectly expresses his contempt for his hypocritical friends Rosencrantz and Guildenstern.