Houses are complicated systems that need to control heat, moisture, water, and power while removing waste. This whole thing needs to be structurally sound while burning slowly enough so that you have time to react and escape in case of a fire. These are the most important investments most people ever make, and for the most part we are all served the cheapest legally permissible house that consumes as much space as possible.
Yep, we should leave that to the market. You happen to live in a building that collapses, well, then you can have The Choice(TM) to never inhabit a building made by the same company again (if you're lucky). Eventually they'll be driven out of the market, unless people choose to risk it after assessing all available information, in which case there's nothing we can really do about it.
That's part of why. Also some rent seeking in there. We used to have weak building codes, more buildings fell down.
We soon learned that a simple timber frame with paneling on both sides meant the whole house was a collection of chimneys, which meant any fire would very quickly overwhelm the entire house.
So we came up with some building codes which said that the timber frame needed more side-trusses to act as firebreaks.
Note that this applies mostly to single and duplex home owners.
I'd agree with you that there is a lot of unnecessary and unproductive bureaucracy in the process, but I do not mind the status quo.