For the most part, your money is safe in a brokerage. Brokerages are heavily regulated to make sure that they do not go under.
That said, you should still stay on the safe side and go with a well-respected, large, and experienced brokerage. This will reduce your risk as it will take a lot of mistakes from the brokerage for it to go bankrupt.
On top of that, we have already discussed how brokerage accounts are insured by the SIPC for up to $500,000 per person per brokerage. If you are worried about the default risk, you may want to utilize multiple brokerages.
Just remember that the SIPC insurance only covers cash of up to $250,000. If you have $500,000 in a brokerage account that is made up for $100,000 of stocks and $400,000 of cash, you will only be covered for $350,000 ($250,000 cash and $100,000 of stocks).
Make sure that your brokerage is covered by SIPC insurance if you are going with a firm that is not that well known. Many smaller firms that offer some of the best fee structures and promotions do so because they are not bound by SIPC regulations, and thus are a risky way of investing in stocks.