Is it possible to prolong the youth of life?
School setting is still popular across many genres of anime, and there's a deep reasoning behind it.
At first, you may think that schools are just relatable. The majority of the titles are based on light novels and manga, which are supposedly targeted at young adults, those who are still in school or have just recently graduated, and they can easily understand the pains, sorrows, and joys of the characters that are put in the same settings. Through that relatability the cast of the anime becomes something like an extended friends group that now also includes the viewer. Even the characters who, aside from your regular school activities, like clubs and exams, have to participate in world-saving.
Also, the school setting allows the story to be quite diverse. It doesn't stop with clubs and exams and the occasional world-saving, it also includes walks home together, karaoke outings, cafe dates, and part-time jobs that can deepen the characters, show them from different sides, and make them even more relatable. School kids, surprisingly, have more freedom than adults: They are in the process of exploring the world and finding themselves, which moves the story forward.
But the truth is, those two paragraphs above are a cover-up for the actual reason. Most importantly, Japanese people believe that life is just not fun after high school. This is the last period of one's life when a person is allowed to dream big, to take risks without drastic consequences, to experiment with themselves and their wishes, to spend time with their friends, to learn what place in the world they want to fill. After graduation, the person morphs into a cog in the never-dying machine of adulthood devoid of fun.
Even adult anime lovers prefer the school setting because of that. It's that nostalgia that gives them this feeling of freedom, of a time when they didn't have to think about rising energy bills and joint pains and what to have for dinner every day. Anime set in school helps the viewers go back to that time, when they didn't have many responsibilities and didn't have to make important decisions on a regular basis. It's a form of escapism, in a way, that allows people to feel young again, even if only for 24 minutes a week.
This deep and sad nationwide existential crisis prompts the authors to explore a setting that is familiar to a huge chunk of their audience and that offers a little bit of respite from the daunting reality of adult life.