Simply, because discrimination does not necessarily cause companies to go out of business.
Other answers have dealt with the problem of preventing concrete harm to customers who are being broadly discriminated against, which would remain a plausible justification for such laws even if any such business would inevitably fail, since new discriminatory businesses might continue popping up, and have also pointed out how in certain circumstances discrimination may be economically beneficial. However, another point is that even economically harmful discrimination will not necessarily force a business out of the market!
Discrimination is indeed often a poor business decision. It represents forgoing potential income, and most economic analyses suggest that a discriminatory business will be less successful than a non-discriminatory one. However, being less profitable only will lead to failure in a situation of perfect competition, where no differentiation between products is possible. In this case, the non-discriminatory business will spend less money on the same quality on labor, while also having more positive publicity and being able to take advantage of economies of scale. In an ideal situation, this would drive the discriminatory company out of business. And it is probably true that, in the modern United States, less-discriminatory businesses will be more successful.
The thing is, effectively, discriminatory businesses are not only taking on additional costs to meet the prejudice of their owners or managers, but also offering a differentiated product. Let's consider a much less harmful form of discrimination: exclusive clubs. Groups like Mensa charge money to belong to their organizations based on characteristics like scores on IQ tests. They offer some small benefits, yet they aren't outcompeted by a group that offers those same benefits to the general public. Why? Because the exclusivity is part of what they're selling. A group that offered a T-shirt with their name on it or access to their clubhouse to the entire population wouldn't be Mensa. No group offering the same services regardless of IQ scores can price Mensa out of the market, because they're not selling the same thing.
As a more direct example, a golf club that charges a significant membership fee and won't accept Jews or black people, or a neighborhood that limits homeowners to a certain religion (and yes, both these still exist) are selling an environment in which people can be surrounded by others who look and think like them. In a similar fashion, discriminatory businesses are actually effectively selling their exclusivity along with their physical products. A bakery that won't sell cakes to gay customers may well be responding to the owner's beliefs, but from the perspective of the market they're also selling a place where customers don't need to worry about seeing two people of the same sex holding hands. This is compounded by there being correlations between different
things that a company can sell: for instance, Chik-Fil-A isn't just selling an environment hospitable to people who oppose same-sex marriage and inhospitable to LBGTQ people, but rather a conservative Christian environment generally, this last one more explicitly.