Basically, the effects of exercise on weight are not limited to muscle gain. Fat is also lost and thus weight is lost. Whether you lose or gain weight would be proportional to the type and level of exercise you do. Yes, there are a large number of people for whom no amount of exercise appears to be prevent weight gain and, ultimately, obesity but it certainly does (along with the other healthy lifestyle factors) prevent excess body fat. Which will help prevent obesity in the majority of the population.
Other than that, exercise can also be used to lose weight in non obese people. I live in rugby country and it's the norm to see rugby players ending their careers and using running to bulk down. Losing muscle but not having it turn into fat.