The short answer, is that there is far more to F1 than just engines, so even if a team is losing some competitiveness by selling their engines to a rival, they still have other areas where they can compete.
In F1, engines are very expensive, typically around $3M per unit. Imagine 4 engines for each car during the season, that's $24M alone for each team to spend. But having said that, by far the biggest cost regards engines, is the development cost, you can easily spend $100M developing an engine, so most of the smaller teams buy their engines, as on their budgets developing their own is simply not possible.
A lot of the midfield and smaller teams survive on budgets of under $100M a season, some on around half that amount, so they are forced to buy their engines.
With recent changes to F1, trying to cut costs to make the sport more feasible, the engine manufacturers are encouraged to sell their engines to the other teams.
Mercedes, Ferrari and Renault have all sold their engines to other teams in the past.
It's difficult to say if the engines sold are identical or not to those used by the "works" teams - officially they are identical.
However, in 2014 the Mercedes team radically changed the design of their engines and their customer teams, Williams, McLaren and Force India, didn't find out until very late and so this may have compromised their car design:
The "works" team always has a bit of an advantage over the customer teams for this reason, they can plan ahead for engine changes and they get a head start in designing their car around the engine.
For the team selling the engines, they get a bit of extra revenue to pay for all that R&D but also they get extra data back from their customers so they can refine the design, maybe iron out some glitches that may make it sub-optimal in terms of performance and reliability.
But engines are not everything in F1. Last season Mercedes were generally accepted to have the best engine while Red Bull struggled with the Renault engine which was regarded as being well down on power. However, at Singapore for one, Mercedes struggled and Red Bull were competitive. Chassis and aerodynamic design are also very important. Even if Enzo Ferrari did once say "Aerodynamics are for people who can't build engines!"
You usually find the works teams do well not only because they have their own engines, but because they tend to have most money as well!
Now, if Honda can sort themselves out they may mean McLaren can challenge the front-runners, but not for another season or two probably.
This is all relevant to the health of F1 as the smaller teams cannot compete with the bigger teams and it's a vicious circle as you need money to compete but need to be competitive to earn that money. There is talk of a price limit on engines that would help a little bit.
Ultimately, if there were no manufacturers selling engines to the teams, the sport would die, or it would be Ferrari vs Renault vs Mercedes vs McLaren (currently) with just the 4 teams on the grid - unless you go down the route of GP2 for example and make it a spec series with all the cars being identical.