Actually, there are many laws concerning that exact situation, right down to how many free beds must be available, especially if you accept any government money, eg, Medicare or Medicaid.
My guess is that most illegals don't have family doctors, so when someone gets an upset stomach, they go to the emergency room. It' expensive for the hospital, but free for them.
The ER is a convenience for admitting patients--one advantage of having an ER.
The real reason there are as many ERs still operating is one you'd likely disbelieve. It is tradition, part of the mission of a hospital to provide emergency care to a community. But when the community will not compensate the hospital, when that community is quick to blame the ER for all the problems that come from the nature of such a service, when the community will not secure the facility from marauding thugs...it can be shut down. And they will continue to be shut down. Once enough hospitals lead the way, and break with tradition--other hospitals will follow suit.