If Spain is a country of many regions, it’s also a country of many climates. The best time to visit depends on where you're going and what you're planning to see. The high central plains (which include Madrid) suffer from fierce extremes – stiflingly hot in summer, bitterly cold and swept by freezing winds in winter. The Atlantic coast, in contrast, has a tendency to be damp and misty, with a relatively brief, humid summer. The Mediterranean south is warm virtually all year round, and in parts of Andalucía it’s positively subtropical – it’s often pleasant enough to take lunch outside, even in the winter months. On a general holiday or city break, in most regions spring, the early part of summer and autumn are the best times to visit. Temperatures will be fairly clement, sites and attractions open, and tourist numbers relatively low – worth considering, especially if your destination is one of the beach resorts or cultural attractions.
Spain is one of the most visited countries on the planet – it plays host to about sixty million tourists a year, rather more than the entire population – and all main tourist destinations are packed in high summer. Even the Pyrenean mountains aren’t immune, swapping winter ski crowds for summer hikers and bikers. August is Spain’s own holiday month – when the costas are at their most crowded, though inland cities (including Madrid) are, by contrast, pretty sleepy, since everyone who can, leaves for their annual break.
Lastly, don't forget the Spanish Canary Islands - pretty much perfect for visiting at any time of the year.