If you’re going to a popular site, you may need or want to buy tickets in advance. For example, Machu Picchu limits the number of people allowed at the site every day so if you only have a small window of availability and during peak season, buying a ticket in advance is probably best. But, if you’re going at off times, advance tickets may not be necessary.
I’d recommend looking to buy tickets to the sites that are limited and are must do’s on your list. If you’re trying to snag an elusive reservation at a restaurant, get the reservation and then plan around it. For most sites, you won’t be needing to buy tickets early at all and can just show up.
If you do decide to go the day of and see there is a massive line to buy tickets or they say that tickets are sold out for the day, all may not be lost. Try calling the ticket office to see if you can buy a ticket for that day and then pick it up at will call. Although the sign outside the ticket office may say sold out, the agent on the phone may not be aware of that or may be able to find space for one traveller. This isn’t always foolproof but it has worked for me. At worst you’ve spent a couple minutes and at best, you’ve saved yourself a frustrating wait.