My feeling is that Hakase's answer is half-correct. I agree that characters are generally chosen so that the target audience can relate to them. However, I disagree that means that they're necessarily the same age as the target audience. That is to say, people don't necessarily relate best (as a group) to people who are of the same age as them. What people do relate well to are characters who are in a situation that is at least somewhat similar to what they have experienced, but this generally only means that the characters need to be younger than the target audience, not the same age.
To understand this in more depth, we need to know a bit about anime/manga demographics. You may already know this, but I'll write it here for completeness. There are five key demographic groups. The youngest group is called "kodomo", literally meaning "child". The target age range is something like 3-7 years old. It is not typically segregated by gender. The most famous show targeting this group is probably Doraemon, but there are actually a large number of shows aimed at this group. You probably don't watch many of them, though, so I'll ignore it. Next, there are shounen and shoujo, for (respectively) boys and girls. These target school-age children in the range of 8-17 years old or so. Most of the most famous anime are shounen, including Dragon Ball, One Piece, and Naruto. There are some famous shoujo series like Sailor Moon, but fewer than shounen. Above that, there are the seinen and josei groups, targeting (respectively) men and women in the 18-34 age range. By volume, seinen is probably the largest demographic group, but certainly not by viewership. You can probably find more information here in a number of other questions about the relative sizes of these and what counts as what. All the ages quoted are approximate, but they're at least moderately accurate.
These demographic groups aren't as flexible as you might think. Most anime is pigeonholed into one of them. Children's anime typically airs during the daytime and is sponsored by the station directly. Most of these aim to be at least marginally educational. Shounen and shoujo anime mostly airs during primetime hours or in the mornings, when they aren't at school. The successful ones are able to secure some external sponsorship and rely on this as their primary source of revenue. Adult anime usually airs in late night time slots (or on special anime stations), when broadcast restrictions are lessened and stations are willing to sell out their airtime. These are usually not sponsored in any way, and actually have to buy their airtime. They rely on merchandise sales, including DVDs and increased manga sales, to break even, and hence are forced into targeting consumers with enough money to purchase the expensive DVDs (typically approximately 6000 - 8000 yen for 2 or 3 episodes) and merchandise. While it's possible for a show to air at a different time than this would indicate (e.g. shounen shows airing in late night slots), it is much harder for various reasons, so it's not that common.
For now, I'll restrict to the male audiences, which are significantly larger than their female counterparts (and with which I have significantly more experience). Among shounen shows, it's actually rather easy to find examples where school is nowhere to be found or irrelevant. All the famous examples I listed above don't have school settings. On the other hand, many seinen shows are set in schools. My personal feeling is that it's probably more common for seinen shows than shounen to be set in a school. I don't claim to have anything like a representative sample, and it may be that the two rates are actually about equal. But even still, the fact that any seinen shows are set in schools casts doubt on the theory that the characters are meant to be the same age as the viewers, since very few members of the seinen target audience are still in high school (and none would be in middle school or lower). By my very cursory counting of my own (not at all representative list), of 16 seinen shows I'm watching this season 11 are set at least partially in schools, which is far more than can be explained by the age of the target audience alone.
Instead, I think the driving force here is nostalgia. Shounen shows generally try to appeal to younger kids desire to be older and more mature (for which they may use physical strength as a proxy). Hence you see a lot of "adult" characters. By contrast, seinen shows aim to appeal to the desire of adults to return to a more carefree and easy-going lifestyle which they experienced when they were younger. Such shows typically present an idealized depiction of school life. It's a school life very few people actually experienced, but it's at least close enough that it serves the purposes of creating a nostalgic setting on top of which the story and characters can be developed. Of course, making the characters younger means they have more room to develop as well, at least if they are realistic.
There's a second, related reason. It's much easier to appeal to the lowest common denominator than to aim for the middle of the road. Putting the characters in high school is not going to demoralize anyone or make them feel inferior. Most Japanese people go to (and graduate) high school; the graduation rate is approximately 95%, tied for third place worldwide. There's little worry that setting an anime in high school will alienate viewers. On the other hand, Japan's college graduation rate is only about 53%; while that's still very high compared to most other countries, anime producers often won't risk alienating 47% of their audience by setting shows in college. Likewise, setting shows in a workplace environment risks alienating NEETs who are, at least stereotypically, often anime fans, as well as people working in very different kinds of jobs. While some series drop this entirely and aim for a completely unfamiliar setting, this is somewhat more common in shounen series that aim to appeal to the viewer's sense of adventure. Incidentally, for much the same reasons, shounen protagonists aren't the brightest lot either. This is absolutely not to say that anime are never set in workplaces or in colleges (see, for instance, Servant x Service or Golden Time), but it's far less common.
Let's take a case study. One of the most successful anime in history is K-On!, a seinen slice of life series about girls in a high school light music club. The 2009 anime was highly successful and drew a number of readers to the manga, which before that point was relatively unknown. The 2010 sequel concluded the series with four of the five main girls graduating high school and going off to college, while the fifth entered her final year of high school.
At the conclusion of the anime, the manga was basically at the same point. However, as something of a surprise, the manga author did not decide to conclude the manga there. Rather, he decided to continue it with two series. One would focus on the main four girls in college. The other continued the story of the remaining high school student in her last year and promoted two previously minor characters to members of the club.
The reaction to this was almost altogether negative. Most die-hard fans did not want a continuation in college. On various internet fora, comments were made. Some people said things like "I don't want to read about a bunch of college girls when I didn't graduate college". Others said "there is no way this can continue to be believable, as now that they're in college, they'll have to get boyfriends, and would not have the time to be in a band together". People just didn't want to read about a bunch of college-aged adult women sitting around and eating cakes, even when they were quite happy with it when the girls were just a year younger. The high-school part wasn't plagued with the same level of negative commentary, but it didn't do particularly well on its own, having lost many of the good characters. Readership dropped drastically, and the manga concluded just a year later, the mangaka apparently having decided too that continuing any further wasn't worth it.
I'm returning to the female demographics because they provide some interesting contrast. At least stereotypically, many shoujo series are romances, set in school. This is perhaps understandable. In fantasy, action/adventure genre shows which are common among shounen series, the unusual setting is a large part of the appeal. However, for a romance show, the appeal is in the characters, and as a result setting it in a familiar place like a school is probably the best option. Indeed, for most viewers in the target audience, the majority of all of their social interactions would take place at school. As a result, many (probably more than for shounen) shoujo series are set in schools.
However, an interesting thing happens with josei anime. Josei is significantly smaller than any of the other three demographics I've talked about here. It's actually quite difficult for josei shows to be successful. But as a result, josei series are, in many cases, forced to be more experimental. These series often feature adults in the workforce or college students, sometimes in unusual or difficult situations. Josei is known for being particularly drama-based; romance is present, but often just to emphasize the interpersonal conflicts. Some examples of successful josei anime which do this are Usagi Drop and Hachimitsu to Clover. One can find even more experimental works in manga, though very few make it to anime. Thus, if you want to find some anime that is really broadly different from the mainstream shounen/seinen/shoujo stuff, you can probably find josei works which will appeal to you.
So, at the end of the day, yes, school settings are chosen because viewers can relate to them. However, that is not because the viewers are expected to be in school themselves, at least not always. Rather, in many cases, it's because the viewers are expected to have been in schools at some point, and hence can relate. There are few options as broadly applicable as primary/secondary education for that purpose. Such settings also often are chosen to invoke some nostalgic feelings, and because they won't alienate most viewers. While one can find anime which are not set in schools, they're necessarily more experimental as such settings are not as well developed and appeal to smaller viewerships. If course most anime is not meant to be experimental, and even most experimental shows don't experiment with everything, so the majority of shows nowadays are set in schools, but there are exceptions if you look for them.