Maybe they’ve changed the rules since then, but at least my personal experience was the flow was to establish whether I was a Spanish or EU citizen, as I was not I was obligated to pay a sizeable deposit before even being seen for triage. There was certainly no indication any other foreigner would have been treated differently.
That distinction can be in addition to, but will more often wholly replace a distinction between citizens and non-citizens.
You can make a purely economic rationale for this if you want: Residents pay tax, and non-residents don't (in the US some non-residents still pay tax but that's not usual for any other country). If your universal healthcare is largely paid for as an "insurance" scheme, such residents are probably obliged to join the scheme, so it makes sense they'd also get the benefits.
I assume if you're in Spain, with no proof of residence you'll be asked to pay?
Do you recall which hospital was that? Sounds like you were sent to a private hospital.
My experience may not have been representative then, as it could have gone differently in a public hospital, but I am unsure. It seemed based on what I was told at the time that I could only seek treatment at hospitals that accept foreigners except in the case of an emergency severe enough to require an ambulance (I called an Uber to take me). I think any hospital would have ensured I was stable but not admitted me for multiple days of care other than one with a foreigner's department. But, it may be that a public hospital would have treated me without any concerns about my citizenship or residency.
In some countries I visited the foreigner's department was more involved in handling currency exchange and payment details, but less concerned with care or providing services (Czech Republic was this way at FN Motol for instance: https://www.fnmotol.cz/en/samoplatci/).
If you are a resident citizen of an EU country, you can often seek treatment in other EU countries under the same social medicine scheme they have and your citizenship/residency paperwork covers you. I'm not sure if Country A bills Country B or how that works exactly, but it was pretty clear that the foreigner's department concept was for non-EU citizens who were private pay and ineligible for social medicine.