Firstly Macchu Picchu is open everyday of the year so you don’t need to worry about booking and finding it is closed. The Inca Trail does close for February as this is the wettest month but Macchu Picchu is still open.
May to August is the best time to visit with dry weather. These are the 4 months with the least rain fall- June is the driest month, followed by July, May then August. The temperatures are in the mid 20’s during the day but are cold at night getting down to 9-10 degrees.
The main tourist season is June to August so it will be very busy at this time and you will need to book ahead to reserve your trains, tours, hotels etc I very much recommend not trying to do Macchu Picchu in a day but having an overnight so you can explore the ruins once the day tourists have left.
The shoulder season is April to May and then September to October. The weather is a little wetter with October wettest then April, September and May. Temperatures don’t change much during the day so it is still mid 20’s during the day and becoming slightly warmer at night-averaging 11-12 degrees.
The wet season is November to March with February being the wettest month followed by March,January,December then November. The Inca Trail closes in February as it is simply to muddy and wet to walk.
The wet season can also result in more cloud cover over Macchu Picchu obscuring your view.
I think the perfect month is May as it is out of wet season and out of the busiest peak season so is a combination of good weather and less crowds than peak season.