The vast majority of Hamlet takes place in fictional royal castle in the city of Elsinore in the country of Denmark. The fictionalized historical location is Kronberg Castle located in the real town of Helsingør. Although the ancient events, preserved in oral tradition, thought to be the inspiration for Shakespeare's tragedy were first written down in the early Middle Ages, in the 12th century, around 1180, Shakespeare's play is set in the late-1500s because there are allusions to the Protestant Reformation, which occurred in Wittenberg, Germany, in 1517. Thus Hamlet is set after the Reformation, although Reformation ideas are still young.
William Shakespeare's Hamlet seems to have been based on an old Danish legend, "Amleth," having an oral tradition that is ancient, about a young prince who avenges his father's death when it turns out that his father had been murdered by his own brother. Shakespeare often set his plays in the past of other kingdoms for reasons of national allegiance. To comment on universal human issues, he set his stories in non-English countries and in times long before his. Hamlet's Denmark is an example of this. The setting of Denmark is also important because part of the plot involves an ensuing war with the Norwegian forces. Historically, Denmark and Norway were once one united political state. Although their political and economic spheres broke apart, they still remained very similar cultures. There were often clashes between the two countries. Shakespeare was very aware that his English audience would like to feel superior against the Danish people, and thus showing political turmoil in Denmark would have pleased England.
More specifically, the settings within and around the Elsinore Castle lend a lot to the themes and atmosphere of the play and its various scenes. The first scene opens up on a platform in front of the castle, where sentry guards are keeping watch. This dark, cold, and slightly creepy setting perfectly sets the scene for the ghost of King Hamlet I to appear. Then the scenes shift inside, to the royal room of state, a semi-public place for government officials to discuss politics, war, and other big issues. The next few scenes are also in other rooms of the palace, which are presumably very ornately decorated. Another important scene takes place in the bedroom of queen Gertrude, Hamlet's mother who has married his uncle very quickly after his father's murder. Hamlet is angry at her for this, and this private and intimate setting sets the scene for their very personal confrontation.
After this, for a while, Hamlet is banished to England, but we never get to see him there. The scenes continue to be set in Elsinore, Denmark. There is even a brief scene on a plain in Denmark, where Fortinbras, the prince of Norway and a political enemy of Denmark, discusses marching on Elsinore and starting a war. When Hamlet returns to Elsinore, a very important scene in the Elsinore churchyard and cemetery happens. While he stands amongst grave stones and dead bodies, Hamlet thinks about death and morality. The eerie and sad setting matches these thoughts very well. The very last scene, in which the majority of the character deaths take place, is set in a castle hallway of Elsinore.
The answer is Denmark.
One way to know where Hamlet takes place is to look at the full title of the play. It is The Tragedy of Hamlet, Prince of Denmark. The action of the play takes place in and around the castle of the king of Denmark.
At some points in the play, the characters refer to action going on in other places that has a major impact on the play. For example, we know that Fortinbras, nephew of the king of Norway is gathering troops in that country to attack Denmark. However, no scene is actually set anywhere other than in Denmark.
The play's full title answers the question of setting: Hamlet, Prince of Denmark. The action takes place inside the castle in Elsinore, Denmark. The name of the castle is not mentioned in the play, but it is acknowledged to be the real Kronberg Castle located in the real town of Helsingør, called Elsinore in Shakespeare's play.
The major setting of the play takes place in Denmark, though characters travel to England and other places. The time is the late medieval period. The atmosphere and mood is dark, violent, passionate, and extremely reflective. Hamlet is a Prince of Denmark, so his title should give you a clue. The mood and atmosphere takes a little unraveling of the plot.
All throughout the play, references are made to Denmark and the area around Denmark. Shakespeare was very thorough in allowing his plays to cover vast amounts of territory and provide a location that any member of the audience would be able to identify with.