The themes of spying and deception in Hamlet are helped along significantly by the setting. In general, the castle provides great opportunity for much eavesdropping. A closer look at specific scenes highlights even further Shakespeare's use of setting to achieve these themes.
A scene demonstrates the significance of setting is the gravedigger's scene. What better place for Hamlet to espouse his existential ideas on the meaning (or lack thereof) of life than in a graveyard surrounded by bones of the dead?
The why of the setting comes from the text Shakespeare used as raw material for his play, which was a historical account of Denmark written by Saxo Grammaticus in which Saxo wrote down the oral tradition of Amleth, the tragic Danish prince. That's where the story came from, and viewers might have been familiar with the story as with other stories Shakespeare adapted.
Now, as far as the significance, that's different. Look at the smaller setting: the castle, enclosed, a tight world where everyone's around one another and always on display. Hamlet grew up there, and knows it well, yet he finds it subverted and changed through his uncle's action. The familiar and known setting is completely transformed, making this a truly weird place for him.
The setting is defined by the origins of the tale which are to be found in Saxo Grammaticus's Historia Danica, but I think also that it is important because it buttresses the feeling of dissension.
We must not forget that the king used to represent the divine authority on earth. Thus, his castle is the throne of that power. Being what it is, that place must remain pure, never to be corrupted or stained. Nevertheless it becomes the theatre of murders, poisoning, betrayal and the very place reeks of corruption, symbolized by Polonius's hidden body.
Thus, the "throne of power" is totally disrupted, it loses its sanctity and is corrupted. The result is the spreading of an atmosphere of anarchy everywhere in the kingdom (remember the war taking place) that goes hand in hand with Hamlet's apparent insanity and the anarchy swaying his mind.
To put it in other words, if the royal castle is corrupted and disturbed, so is the whole country. And this overwhelming insanity intensifies the effect of the madness of Hamlet on the reader. By the way, Hamlet is the one who is supposed to bring back traditional order, yet he is in chaos over traditional order as it conflicts with the new order he learned in Wittenberg, and we have to wait for another, a traditional saviour at the end of the play.