I have a valid ESTA.
If my visa waiver 90 days end on August 1, and I have a ticket leaving the US on the same day, would it be OK or should I purchase a ticket a day before?
Technically your status is valid until the end of day on the date stamped in your passport - in this case, that would be 11:59pm local time on August 1st. As long as you leave the country before that time, you're OK.
However you need to be very careful. The US is very unforgiving when it comes to overstaying your visa, and overstaying even 1 day (even if it's just 12:01am on the 2nd!) will result in you being unable to enter the US using the Visa Waiver Program ever again, and you will require a visa for future entries.
What's more, it doesn't matter why you overstayed. Your flight being delayed a few hours pushing it past midnight, or your flight being canceled due to weather and having to fly out the next day are NOT valid excuses for overstaying.
The US does not have physical immigration on exiting the country in most locations (including airports and many border crossings), so even the fact that you're already in the airport will not necessarily be sufficient in terms of having "left" the country.
So the short answer is that if everything goes well, then you're fine and legal. But if the weather is bad and your flight is canceled, then you could find yourself in a world of pain. The normal recommendation would be to plan not to stay more than about 88 days, but it's a bit late for that unless you decide to change your return flight.
You need to travel before midnight of August 1st. This is because on the 2nd your status is invalid, not the first.
I would not cut it too close, though.
Your visa is valid for a full 90 days. Therefore, right up to a minute before the end of the 90 days, you can stay.
You haven't specified what type of ticket you have, so...
If you are flying out, that's easy enough as you'll pass immigration and can do so in time, no matter whether or not your flight leaves on time.
However, if you're leaving by say, bus to Canada or Mexico, be aware that your bus might leave, say, Seattle at 11pm, but you won't cross the border into Canada until after midnight. Even a 9pm bus might hit traffic or be otherwise delayed. In this case (as I've seen happen elsewhere) you'll have technically overstayed and may get penalised.
So it's best, if possible to give yourself a buffer and get out before the end of the 90 days, but legally, you're entitled to use all of them, unless told otherwise by the US.