The purpose of setting in the context of a narrative is to ground a story in the reality of place. Note that I don't say to ground a story in reality; the use of setting would be rather poor if one constrained themselves to writing only what made sense in the context of our reality.
To avoid going too deep into all this shit nobody but me actually cares about, the purpose of a setting is to provide a backdrop to, and support the development of, a narrative. With the setting, you set the time and place of a story, you design the milieu, you develop history, culture, and use this all to build a larger narrative- it informs character dialogue, tone, atmosphere, character interactions, social context, and provides a foundation for the metaphor most narratives are built around. It serves the purpose of building verisimilitude, of providing context for theme, and style.
It serves a great many purposes, that any writer worth their salt (i.e. pretty much nobody on SV, through no fault of their own) know when to manipulate and build upon and discard, much like with characterization, tone, atmosphere, themes.
Because, see, here's the thing?
As a rule, a well-developed setting is integral to building the verisimilitude a decent story works on.
As a rule, a decent writer knows how, when and why to break the rules that govern writing to make a story better.
Given this is what inspired this thread, I should probably at least quote this post here.
In the hands of any decent writer, the setting is an integral part of the story. You've probably heard the expression "the setting is almost a character in its own right", yeah?
In
Worm, to use an example just about everybody here will have heard of, Brockton Bay is almost a character in its own right. The architecture, the design, the atmosphere, the tone, the characters and social context that pervade the very essence of the town breathe
life into it. In turn, the characters shine in the setting, their distrust and anxieties and petty hatreds amplified in the reader's mind by the setting.
Neither would work half as well without the other; Brockton Bay was crafted to have a specific tone, feel, to it, and that feeling was then used to amplify the attributes of the characters within, which played in to the narrative and etc etc.
Or, to use an example probably far fewer people here have heard;
A Casual Vacancy, a non-Harry Potter book written by J.K. Rowling. In it, Pagford's setting is informed by its social context- it's
ripe for small-town bickering, nonsense, prejudice and pettiness.
In
A Casual Vacancy, the setting isn't actually a physical place (well, technically, Pagford is a physical place, but it's not the part of the setting that actually influences the narrative). Rather, the setting boils back to the small-town attitudes that pervade smaller towns in places like England and America, where rumours can tear a person's life apart and people are small, shrill, selfish and slutty in equal measures. There's a broad context behind this that has developed through decades of media exaggerating and portraying this life, and the story uses this to
excellent effect in how it highlights its character's deficiencies, issues and overall petty grudges leading to the self-destruction of the town.
(Of course,
A Casual Vacancy is a very problematic story, and it's not actually very good; I'm using it to highlight the use of the setting within here, I'm not calling the rest of the story good.)
"The stage in which the story is told" is a very, very simplistic view on what the purpose of a setting is and should be, and misses out on a damned
ton of nuance that has helped some very decent authors craft some amazing stories through careful manipulation of their setting and the atmosphere derived from that.