Access to free services from the Spanish healthcare system will depend on your residential status in this country. If you are a resident or have a working visa, you will have the same rights as any Spanish citizen. This means you have access to all services in the same way a Spanish national would because you are paying regular contributions and taxes towards your healthcare. If you are a non-resident but come from a country that is part of the European Union you will be able to benefit from healthcare temporarily. You will need to hold a European Health Insurance Card (EHIC) to be granted free healthcare during your stay. However, make sure to contact the consulate or embassy beforehand to be aware of the regulations.
If you hold any other nationality, you might still be able to benefit from free healthcare. If you are British you can apply for a Global Health Insurance Card (GHIC) through the NHS. This will grant you access to any medically necessary healthcare in Spain. Other countries may have specific schemes with the Spanish government that allow you to use healthcare at a low price. You should contact your local authority to get further information.