Spain was invaded by Islamic armies in the 8th century AD, and for a short time these armies even threatened to invade France. It would take over seven hundred years for the various Spanish kingdoms to finally defeat and expel the last Moores from the country. For the Christians it was a long drawn out process to reconquer all of Iberia, though Portugal was liberated relatively earlier. The Spanish tended to make small gains, then build and garrison castles to secure those gains. The victories of El Cid instilled more confidence into the Christian kingdoms yet did not particularly hasten the successful conclusion of the Reconsquesta.
The Reconquesta did much to ensure that Spain had highly trained soldiers, and some of the most effective in Europe. During its course Aragon and Castile had become the most powerful Spanish kingdoms, that were jointly ruled by Ferdinand and Isabella. In 1492 the Catholic Monarchs as they were called ordered the final successful assault on the last Moore stronghold of Granada. The fall of Granada marked the end of the Reconquesta. Of course 1492 was significant for Spain due to Columbus discovering the New World. The combination of New World bullion and the Habsburgs ruling in Spain would lead the country into its Golden Age and some of its most historic wars.
Spain entered its Golden Age at the beginning of the 16th century, and would spent most of it being involved in wars. Ferdinand and Isabella had formed alliances with the Habsburgs and with England via dynastic marriages. This alliance system was mainly aimed against France. In 1498 the French invaded Italy, and that started the Italian Wars, which did not end until 1559 (with short truces in between). The Habsburgs and the Spanish kept defeating the French, who kept trying after each defeat.
Charles V was already Holy Roman Emperor and the ruler of the Netherlands when he succeeded Ferdinand and became Charles I of Spain. It was mostly Spanish money and troops that Charles relied on in the conflicts across his vast empires. The Spanish continued their significant part in the Italian Wars, especially the stunning victory at Pavia in 1525. No Spanish troops were involved in the sack of Rome two years later that demonstrated Charles had complete control of Italy. For a while the alliance with England lapsed due to the latter breaking away from Rome.
The Reformation would get the Spanish involved in more wars, and make the political situation across Western Europe more complex. As fine as the Spanish infantry was, the commitments of Charles put a strain on Spanish forces, and even the vast treasures from the New World could not pay for all the wars. When Charles abdicated in 1555, he left Spain and its empire to his son Phillip II, and the Holy Roman Empire to his brother, Ferdinand II.
Phillip II did manage to bring the Italian Wars to an end yet that did not put an end to the fighting. Phillip had to contend with Ottoman Turk advances, although naval victory at Lepanto, and the failure of the Ottomans to take Malta meant the status quo remained intact.
The Netherlands would be the scene of a conflict that did much to dent Spanish power. Spain got involved in attempting to keep hold of all of the Netherlands as the 80 Years War began. The heavy handed handling of the revolt backfired badly on the Spanish. Phillip also got involved in the French Wars of Religion as he tried to prevent Henry IV gaining the French throne. This led to the war with England and the disaster of the Spanish Armada.
From decline to the Spanish Civil War
Spain was still a major power when the the 80 Years War finished in 1648 yet it would decline, particularly in relation to England, France, and the United Provinces (the part of the Netherlands it had lost). The English navy made Spain bankrupt went it sank the entire treasure fleet in 1657, which meant thee Spanish had to accept peace terms. In the rest of the 17th century and 18th century Spain tended to only fight wars if allied to the French. The extinction of the Spanish Habsburgs led to the War of the Spanish Succession. In the end a Bourbon did become King of Spain, but the French and Spanish branches of the Bourbons were not allowed to take control of the other country.
Spain was allied to the French in the Napoleonic Wars until its people revolted, and were helped by the British. After losing its Latin American colonies and then Cuba was a country in decline.
Unfortunately the war Spain is best known for was the Spanish Civil War of 1936 to 1939 when the military managed to overthrow the Republican government.