You may not be able to get your first choice, but you will definitely be able to find a place to stay. Even still, when I was there in may (which is a peak time) you stood a good chance of getting into the popular hostels just by showing up. Sometimes you would have to drop your stuff and come back to see which reservations didn't show up though.
I would book 1 day ahead if possible, and don't worry about it if you can't. The hostel staff should be able to help you find a place to stay if they're full. There are plenty of very low cost hotels that are similar in price to hostels...it may be really basic, but there should be a place.
As an example, my friend and I ended up spending the night in a tiny border town because the border to Bolivia was closed. Someone pointed us to this place:
As you can see it doesn't look like much outside but its clean and comfortable...super cheap, maybe 15-20 soles. Shared bathroom, no heat, no hot water. It helps if you know spanish to find places like this, the Peruvians we were riding with didn't think we'd be ok with it, so you have to do some convincing to get pointed to these types of hotels. I think there are always places where they'll let you stay that aren't heavily trafficked.