Spain is a member of the Schengen Agreement.
There are no border controls between countries that have signed and implemented the treaty - the European Union (except Bulgaria, Cyprus, Ireland, Romania and the United Kingdom), Iceland, Liechtenstein, Norway and Switzerland. Likewise, a visa granted for any Schengen member is valid in all other countries that have signed and implemented the treaty. But be careful: not all EU members have signed the Schengen treaty, and not all Schengen members are part of the European Union. This means that there may be spot customs check but no immigration checks (travelling within Schengen but to/from a non-EU country) or you may have to clear immigration but not customs (travelling within the EU but to/from a non-Schengen country).
Airports in Europe are thus divided into "Schengen" and "non-Schengen" sections, which effectively act like "domestic" and "international" sections elsewhere. If you are flying from outside Europe into one Schengen country and continuing to another, you will clear Immigration and Customs at the first country and then continue to your destination with no further checks. Travel between a Schengen member and a non-Schengen country will result in the normal border checks. Note that regardless of whether you are travelling within the Schengen area or not, many airlines will still insist on seeing your ID card or passport.
Nationals of EU and EFTA (Iceland, Liechtenstein, Norway, Switzerland) countries only need a valid national identity card or passport for entry - in no case will they need a visa for a stay of any length.
Nationals of non-EU/EFTA countries will generally need a passport for entry to a Schengen country and most will need a visa.
Only the nationals of the following non-EU/EFTA countries do not need a visa for entry into the Schengen Area: Albania*, Andorra, Antigua and Barbuda, Argentina, Australia, Bahamas, Barbados, Bosnia and Herzegovina*, Brazil,