Madrid is hot and dry in summer—with temperatures reaching 95°F to 105°F in July and August—and chilly in winter, with minimum temperatures in the low 30s or slightly below in January and February, though snow in the city is rare. The most pleasant time to visit the city is spring, especially May, when the city honors its patron saint and bullfighting season begins. June is also a good month, as are the last four months of the year—in September and October it's likely you can still wear a T-shirt.
If you can, avoid traveling to Madrid in July and August—especially August, even though fares are better and there are plenty of concerts and open-air activities. Many locals flee to the coast or to the mountains, and as a result many restaurants, bars, and shops are closed for vacation.