The phrase conveys Hamlet’s inner emotional state and philosophy. Hamlet is depressed because his father died. Similarly to the state of Denmark, he is also physically and mentally constrained. It is due to Claudius usurped the throne and married his mother. These two factors summarize the meaning of the phrase.
To understand the analysis, the reader has to know the context. A dialogue between Hamlet and his acquaintances precedes the phrase. Hamlet asks what brought Guildenstern and Rosencrantz to prison. They ask the opposite question about which jail in question. By prison Hamlet means Denmark. His friends answer him that then the whole world is a prison. Hamlet agrees with this statement but notes that Denmark is the worst of them. Guildenstern and Rosencrantz disagree with him. To which Hamlet says that, therefore, this prison is not meant for them. Then he pronounces his famous philosophical phrase.
Hamlet understands that Denmark is just a place. However, the political situation of his homeland depresses him. Personal tragedy also has a negative effect on him. Imagine this: your father and the only friend are dead, and the fratricide usurper seized the throne. Therefore, Hamlet here can be seen as a personification of melancholy relativism.