Generally, this understanding is taken from what has been narrated about the revelation of a surah or ayat.
However, sometimes there are contextual clues. For instance, if a verse discusses a historical event that happened after the hijrah, such as the Battle of Badr, it is understood that it was revealed in Medina.
Also, the style and content of Meccan verses are somewhat different from the style and content of Medinan verses. The Meccan verses tend to be shorter and focus more on the existence of God, the afterlife, and important ethical points, whereas the Medinan verses tend to be longer and also discuss matters such as legislation. This isn't an absolute rule but also lends some evidence.