One of the key locations of Game of Thrones is the wall at the northern border of the Seven Kingdoms. Three hundred miles long, seven hundred feet high, and built of ice blocks, it is here that the Night Watch hold the line against the ‘wildings’ that lie beyond and whisper rumours of the undead White Walkers. The viewers know by now that these super-zombies exist, that the wall has been breached, and “winter is coming”.

Game of Thrones’ wall evokes Hadrian’s Wall, which separated the Romans from the Barbarians

In George RR Martin’s novels, the historical inspiration is Hadrian’s Wall in northern England. It marked the northern limit of the Roman Empire, dividing conquered land from the unbeaten northern tribes. Although a more modest affair, stretching 70 miles (113km) and rarely standing more than 10ft (3m) high, it was built in about six years from 122 CE. Manned with castles at every Roman mile, Hadrian’s Wall was a mark of imperial power and was intended, as one early chronicle records, ‘to separate the Romans from the Barbarians.’

This has long been the function of walls. Announced as defensive structures and imagined as vast impenetrable barriers, instead they are often gestural markers that serve more social, psychological and symbolic purposes. They help a culture to define itself by keeping what it’s not at bay.

The wall in Game of Thrones might be a fantastical barrier, but this – so far – is the status of that other wall that dominates contemporary debate: Trump’s pledge in the election campaign in 2016 “to build a wall and get Mexico to pay for it”. Although some suggested designs for Trump’s boundary wall have been released, it is still unclear how this vast infrastructural project will be funded and managed. Its purpose, so far, has been entirely about symbolic conceptions of nation and belonging.

Trump’s wall has, of course, many historical precedents. The Great Wall of China was started as early as 481 BCE, although the iconic stone sections date from about 400 years ago. This wall, too, was used to mark the civilised world from the impure tribes beyond. At over 4000 miles long, sparsely garrisoned and sometimes little more than raised earthworks, it was not much use as a physical barrier or defensive structure, but operated more as a symbol that defined Chinese nationhood, which was premised for centuries on ethnic and cultural exclusion. This is why the Communist Party since 1949 has continued to add to the wall and preserve its historical sections as an emblem of the nation: it defines the People of the People’s Republic.

The Greeks called those beyond the city walls barbarians

The Greeks had similar motivations. They called those beyond the city walls barbarians– the word mocking the “bar-bar” stutter of those barely civilised enough to speak. Greek horror deepened the further they went from home. Beyond the known world of the Mediterranean sea were the Anthropophagi, the cannibal men who were little more than beasts hungry for human flesh. This horror still haunts contemporary Gothic fiction: beyond the walls or fences of every final refuge after the apocalypse, zombies mass in indifferent, devouring hordes. On The Walking Dead, the stripping away of all civilisation beyond the walls applies forcefully to human survivors who are nevertheless still strangers, not of the group. Walls have always offered a way of dividing the human from the inhuman.

Divide and conquer

The Cold War era was haunted by the building of the Berlin Wall by East Germany, which was started in August 1961. It was designed to prevent the mass migration of Eastern bloc citizens to the West, the structure being continually reinforced and rebuilt for three decades. Hundreds were killed trying to cross the 96 miles of wall that enclosed West Berlin, and the border zone became the iconic setting for many spy thrillers, from John Le Carré’s The Spy Who Came in from the Cold (1965) to Steven Spielberg’s Bridge of Spies (2015). The Berlin Wall wasn’t just geo-political, but served as a handy psychological emblem of alienation and self-division, as in Pink Floyd’s concept album The Wall (1979).

However, the Berlin Wall appeared at the beginning of inter-continental missiles, mass air travel and global satellite communications, and these made the wall seem increasingly absurd in a seemingly ‘borderless’ world. It became a disastrous symbol of the coercion and control of East Germany over its citizens. Many US presidents used the wall as a backdrop for ideological statements of Western freedom, from John F Kennedy onwards. When Ronald Reagan spoke in Berlin in 1987, his injunction to “tear down this wall” was a crucial moment in ending the Cold War. Two years later, in November 1989, the borders were declared open and Berliners themselves began dismantling a wall that had divided the city for 28 years.

The new millennium has been marked by an extraordinary number of wall-building projects

For a while, in the 1990s, we believed in the rhetoric of a frictionless globalised world, the free movement of money, goods and people. This was what the sociologist Manuel Castells celebrated as “the space of flows”. Yet the new millennium has been marked by an extraordinary number of wall-building projects around the world, in direct response to anxieties produced by open borders. The rhetoric of internationalism and free movement across borders has become very hemmed in by the simultaneous construction of physical barriers that often serve deeply symbolic ends.

Israel began its ‘separation barrier’ in 2000 – the government agencies that proposed and are building it tend to avoid using the word ‘wall’. It was a response to the violent uprising of Palestinians on the West Bank, the Second Intifada, and the barrier was meant to separate the Palestinian population from territory assigned to Israel. It is projected to be 440 miles (708km) long when complete, although the building has been continually challenged in international law. Simone Bitton’s documentary Wall (2004) ends with the everyday passage of people climbing up and over the wall, a silent evocation of how porous the barrier remains. Rather more apocalyptically, World War Z (2013) at first seems to regard the Israeli separation barrier and militarized check-points as robust defences against a zombie virus outbreak, but of course the wall is overrun by undead hordes – just as it is in Game of Thrones.

Concrete canvas

Of course, just as walls serve as barriers they become blank canvases, as well. The graffiti on the Berlin and the West Bank walls has become famous for the expression of resistance to separation. One of the most popular artists of our time, Banksy, has made a career out of stencilling art defiantly on walls. A few years before Trump, the science fiction writer Tom Disch imagined a massive wall built along the US-Canadian border becoming a vast open-air art gallery.

Meanwhile, in 2004, India finally completed their electrified fence along the Line of Control through disputed territory separating India and Pakistan. It is over 340 miles long. The refugee crisis since the collapse of Libya and Syria into civil wars has prompted a whole rash of wall-building to defend European borders, from the Spanish fence around the port of Melilla in Morocco to the various border fences built by the Hungarian government in 2015 and 2016. Nationalist reactions to ‘borderless Europe’ have been a mark of recent elections across the continent. For all the walls, millions of refugees have still made it through the perilous journey.

As for the history behind Trump’s Wall, the border between the US and Mexico was first formally established in a treaty of 1848, and initially marked only by piles of stones and a handful of marble obelisks. The first fencing went up in 1945 to manage the large numbers moving across the border at Tijuana and Ciudad Juarez. The building of a physical border was massively extended following the founding of the Department of Homeland Security and its ‘Secure Border Initiative’ of 2005. Since then, over 650 miles of fencing have been constructed, under both Republican and Democratic presidencies. For all the rhetoric of invasion, deportations have exceeded inward migrants since 2005.

The cultural representation of this southern border has matched this ratcheted anxiety. In the 1940s, Anthony Mann’s liberal Hollywood film Border Incident (1949) imagined benign co-operation between Mexico and the US as the ideal to protect vulnerable Mexicans tempted by people smugglers. In Orson Welles’ noir, Touch of Evil(1958), the film famously opens with an exuberant crane shot that travels across the US/Mexico border in a single, unbroken take. Now, in films like Sicario (2015) or TV series like Breaking Bad (2008-13), the border zone is highly militarised, and the place south of the border is imagined as a demonic world of violence, degradation and terror, where unimaginable torture and cruel death awaits the foolhardy American.

Walls retain their psychological value as demarcations of a dream of purity

In Gareth Edwards’ horror film, Monsters (2010), the border becomes a gigantic wall, hundreds of feet high, trying to fence off America from the vast alien creatures that now roam the land. Of course this defence fails, and the film ends with the American south ‘infected’ by what seem gracious, sublime yet utterly alien beings strolling through an extended borderscape of abandoned towns.

All of which is to say that the mythical power of White Walkers coming from beyond to bring pestilence and death to the civilised Seven Kingdoms in Game of Thrones is hardly out of keeping with our contemporary wall-building frenzy.

Of course, walls remain practically rather useless barriers, rendered increasingly obsolete by new technologies like drones. Yet they clearly retain their psychological value as demarcations of a dream of purity, keeping out all those threats to self-identity. Let’s just hope that with Game of Thrones, The Walking Dead and other stories formerly deemed fantastical that now look allegorical we can satisfy our violent fantasies of expulsion and purification through fiction rather than real life.

If you would like to comment on this story or anything else you have seen on BBC Culture, head over to our Facebook page or message us on Twitter.

And if you liked this story, sign up for the weekly bbc.com features newsletter, called “If You Only Read 6 Things This Week”. A handpicked selection of stories from BBC Future, Earth, Culture, Capital and Travel, delivered to your inbox every Friday.