My ESTA application expires during my trip. Does it have to be valid the whole time I’m in the U.S?

No, it does not. Your ESTA authorisation needs to be valid for when you enter the U.S. and will allow you to stay within U.S. territory for up to 90 days from when you land. Therefore, it is okay if your ESTA does not remain not valid throughout your stay in the United States as long as you do not overstay the limit of 90 days.
It is important to bear in mind that while your ESTA authorisation does remain valid for 2 years or until your passport expires (whichever arrives first), your ESTA will never permit you to stay for longer than a period of 90 days. In order to stay in the U.S. for any further length of time, you will need to obtain a visa.
To provide confirmation of this, the official website of the U.S. Customs and Border Protection agency states the following:
"If the ESTA expires while you are in the U.S., it will not affect your admissibility or the amount of time you are permitted to remain in the U.S."
In addition, 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, your passport must remain valid up until your departure and for six months after your arrival.
You will not need to apply for ESTA again before you leave the U.S., though you will need to apply for a new one if you would like to visit the U.S. again after your departure. This can be done at any point before, during, or after your stay, and you do not need to wait until your current ESTA authorisation period of 2 years (or less, depending on your passport expiry date) runs out. If you have more than 30 days remaining, you will receive a warning message that you still have remaining time on your ESTA, but you can still renew your ESTA anyway if you choose to.