Spain's size means there's rarely a single best time to visit — it depends entirely on where you're going and what you want to do.
Spring (April–May) is excellent almost everywhere. Temperatures are comfortable, wildflowers are out, and the big festivals — Semana Santa, Valencia's Las Fallas in March, and Seville's Feria de Abril — fall in this window. The south is pleasant without the summer heat, and the north is green and mild. Crowds are manageable except around Easter week, when transport and accommodation should be booked well in advance.
Summer (June–August) brings reliable sun and warm water to the coasts, but interior cities like Madrid and Seville can be brutal — Seville regularly exceeds 40°C in July. The coasts are packed in July and August; prices peak and accommodation books out weeks ahead. The Atlantic coast (Galicia, Asturias, Cantabria) stays cooler and green, and is much less crowded. The Canary Islands are actually milder in summer than the mainland.
Autumn (September–October) is arguably the best time for most of Spain. The heat relents, the crowds thin, the harvest season begins in wine country (Rioja, Ribera del Duero), and cultural life picks up after the summer break. September still feels like summer on the Mediterranean coast — warm enough for swimming, without August's crowds.
Winter (November–February) is misunderstood. The Mediterranean coast from Valencia to Málaga stays mild and sunny, making it a good option for a quiet off-season visit. The Canary Islands are at their best in winter — warm, dry, and uncrowded by northern-European summer standards. Madrid and Barcelona are cold but lively; Madrid's Sierra Nevada has skiing from December onwards. The north coast (Galicia, Basque Country) is rainy and cool.
In short: avoid the south's interior in July and August unless you're heat-proof; avoid the coasts in August unless you book months ahead; spring and autumn serve almost everyone well.