The European Health Insurance Card generally covers any citizen of the EU. This card is issued in the home country before the stay.
There are other countries that are not members of the European Union, but with which the Spanish state has reached agreements on health care such as: Andorra, Brazil, Chile, Ecuador, Panama, Paraguay, Peru, Switzerland, Morocco, etc. In these cases, you should contact the Spanish Embassy in your country to obtain more information.
If you are not interested in the previous option (Publich Health System) or if you come from other country, you should obtain a private health insurance that gives you coverage in Spain. The cost can vary, since it changes according the obtained service.
It is very important for the visitors to clarify the health insurance conditions before leaving their country.
If you are Erasmus student, you can have the European health insurance card, which provides access to the normal medical services; if you are not a student of the European community, you can access to the medical insurance that provides the university: