They evaluate the potential for profit and loss on each single transaction. They don't use some customers to subsidize others, or some business units to subsidize others. If they did, competitors would offer better prices to the other customers. (Not always: I worked in a division of a company that made about billion dollars profit in the 1980's, another division lost about the same amount. Sigh.)
Apparently they have decided that writing homeowners policies in NJ entails much more cost and risk than opportunity. It would have to be a really large downside for them to pull out, because once you leave a market it's very difficult to reenter and I am sure the management of All-but-several-States is aware of the negative publicity withdrawing from any market will generate.
Insurance companies are among the most highly regulated companies in the US. Most states stringently regulate what kind of investments insurance companies can make to make more money off of unspent premium. Real Estate, compared to the stock market itself, is among the safest and most lucrative investment options, but like you said, if a particular real estate market goes soft, so does the insurance companies bank account.