You should try to avoid the situation where your ESTA expires while in the USA but if it does happen, it only has consequences if you overstay the 90-day limit. So, even if your ESTA expires part way through your trip, as long as you have not overstayed the limit, there are no consequences to this.
If your ESTA does expire during your trip, this will not negatively affect your future travels to the U.S., as long as you do not overstay the 90 days that the Visa Waiver Program grants you. However, be aware that whilst your ESTA does not need to be valid for the entirety of the duration of your stay.
The reason you should try to plan your trip so that it is not close to your ESTA expiry date is in case your flight is delayed for some reason and your ESTA expires before you get to the US border control. In this case, the airline will usually decline entry onto the plane as they know that you do not have the relevant authorisation to enter the US. If your ESTA is approaching expiry it is a good idea that you apply for a new one before your trip, as it will just supersede the previous one, you don’t need to wait forone to expire before you apply for a new one.