In many areas of the US people choose the place to live based on the
quality of local schools. In districts where schools are
geographically segregated, many parents even resolve to various scams
in an attempt to get their child into the "right" school.
However it seems unclear to me why bad schools exist in the first
place - assuming that the government wants to make sure that all
students receive a good education, shouldn't they replace personnel in
schools which are considered "bad"? Or do bad schools only exist
because the local population is under-educated in the first place and
therefore no amount of schooling can help their children become
better?
A good place to begin is to recognize that in much of the United States there is only one school that everyone attends.
For example, I grew up in small town Ohio and there was only one public high school, there were no private high schools, and the nearest public high school to ours was more than an hour away by school bus.
Traditionally, everyone had one public school to which they were assigned on the basis of where they lived with elementary schools being more numerous and in smaller geographic catchment areas than middle schools than high schools. Many high population density urban metropolitan areas now allow some degree of choice against that backdrop.
School desegregation reduced the impact of a geographic catchment area within a larger school district, but it also led to "white flight" to suburban schools and parochial schools (i.e. church run schools, often Catholic in Northern cities and historically white Evangelical protestant in the South).
Another good place to begin is to understand what a "good" and a "bad" school mean, terms about which there isn't a consensus.
Absolute Academic Achievement
One commonly used definition of a "good" school is one that has high academic outcomes in absolute terms - its students don't drop out, frequently go to college, frequently go to selective colleges if they do go to college, frequently find decent jobs if they don't go to college, perform well on standardized tests, take more advanced classes such as advanced placement, international baccalaureate, honors and college classes, etc.
This is overwhelmingly a function of the socio-economic backgrounds of the students and sometimes selectivity in admissions. Mostly, this means the education and income of students, but it also captures factors like the percentage of students learning English as a second language, and the percentage of students who change schools during the course of the school year due to parental job shifts.
The powerful linkage between socio-economic background and outcomes is a mix of nature and nurture. IQ is strongly hereditary, and parents tend to raise their children in much the same manner that they were raised - which is positive in terms of school success in the case of parents who are successful, and negative in terms of school success in the case of parents who have failed socio-economically and when they were in school because of the behavioral and cultural ways of living and parenting that they learned from their parents.
This varies greatly from school to school, because different school catchment areas have a dramatically different socio-economic makeup. Also, school choice within the publicly funded school arena and private education (both religious and non-religious) often accentuate already existing disparities as affluent middle class kids opt out of local public schools in low income areas with "bad schools" by this measure.
The variation is so extreme due to histories of de jure residential segregation, federal housing policies until the last thirty years or so, and histories of economic development and migration that vary from place to place. The extreme socio-economic inequality of the U.S. usually persists right down to the neighborhood level. The 1% and those in poverty, almost never live in the same neighborhoods, and even if they do, they attend different schools with the 1% opting out of public schools if they feel the need to do so.
Basically, school quality varies because schools predominantly draw from quite narrow geographic areas and neighborhoods are extremely segregated by education, income, race and other demographic factors, by design.
Parents work hard to get into the "right" school mostly in the belief that the "right" school will have more students at a similar level of academic development to their child that is also more safe, and has programs and resources that will foster more student growth as a result. Whether this is empirically true or not, it is widely believed. At the extremes there is some truth to it, in the middle a lot of this perception is not supported by reality.
Avoiding "bad" schools is, by and large, perfectly rational for pretty much everyone who had the means to do so and leaves schools full of children whose parents are unable to find better options or simply don't care. Chasing "good schools" is more challenging and less likely to produce reliable results.
Safety
Another crude way to define the difference between a "good" school and a "bad" school is the personal safety of teachers and students. Any school in which students and/or teachers must fear for their personal safety on a regular basis is a "bad" school.
In general, personal safety turns out to be closely correlated with absolute academic achievement with some caveats.
"Bad" schools from a safety perspective are considerably more likely to be located in poor urban areas with high levels of crime outside the school (with schools generally being safe relative to the outside world in these neighborhoods), and safety tends to be a concern only in the worst schools (perhaps the bottom 10%). The difference in personal safety between the 5th percentile and the 20th percentile of school safety is much greater than between the 20th percentile of school safety and the 65th percentile of school safety, even though the percentile gap is three times as great.
Levels of gang activity in a school are also closely related to its safety, and some schools may be safe for some students and not for others (e.g. a gay student is at higher risk of personal safety concerns in many schools, and black students are at higher risk of personal safety concerns in many rural predominantly white schools).
Value Added
Another way to distinguish "good" schools from "bad" schools is the extent to which students make academic progress during the course of a school year, or from school year to the next school year.
This measure is much less strongly correlated with student socio-economic background, but is not divorced from it either.
Maybe 80% of schools produce students whose performance is what you would expect for the socio-economic status of these students, perhaps 10% have students who over perform, and perhaps 10% have students who under perform.
Almost all of the schools whose students under perform are students at a "bad" school by the other two measures, and experience minimal or negative academic growth from year to year.
Students at "good" schools often (but certainly not always) tend to make above average progress, but this is much more patchy. Plenty of "good" schools by other measures, however, are only fair by a value added measure. There is little pressure to improve academic performance when the student body by dint of having students from affluent, well educated, stable families will perform very well even with mediocre academic instruction at school.
A significant number of schools that are safe but below average in absolute academic performance (perhaps half to the 10% of schools that over perform) are excellent by a value added measure. Schools in this category tend to be schools of choice or private schools because they typically insist on longer hours of school attendance each year and have a greater focus on behavior modification than an ordinary school could manage without meaningful consent from parents. Another group of schools that often over perform relative to the socio-economic background of their students are well focused vocational training schools.
So, while some schools do have a consistently socio-economically uplifting effect and "add value" through instruction that is more than "par for the course" for a particular student starting at a particular place, usually they don't and it is very hard to replicate the success stories.
Role of Funding
Since a significant share of school funding is local, and communities with lots of students from affluent backgrounds tend to be affluent communities that pay more in taxes, "good schools" also tend to be better funded. But, funding is at best a secondary order affect relative to the socioeconomics of the students who attend the school.
In urban central cities, funding levels per student tend to be at least average, but student needs (e.g. for special education for students with learning disabilities and for general support for very poor students such as homeless students) tend to be much greater.
The least poorly funded schools tend to be rural schools in low income rural areas with an insufficient local tax base to support them and with high costs associated with a lack of economies of scale and large transportation costs for students. These schools are limited in the opportunities that they can offer students due to lack of funding, but partially make up for this with personalized attention to students in small supportive school environments and with tightly knit communities.
Appalachia and the rural Southwestern U.S. tend to suffer the most from reduced funding that leads to reduced school quality, as these areas often lack of social capital from the community that compensates for poor school funding in other rural areas.