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

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."

What happens if my ESTA expires while I am in the US?

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, your passport must remain valid up until your departure and for six months afteryour arrival.
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.
If you have had a new passport issued since you applied for your ESTA then your ESTA will no longer be valid. An ESTA is linked to the passport details that you provide at application and an ESTA cannot be transferred from one passport onto another, a whole new ESTA is required.

What happens if I overstay the ESTA 90-day limit?

There are different consequences depending on factors like how long you overstay the 90 day limit and the reason that you have overstayed. People who choose to stay in the US without a valid visa are classed as illegal immigrants and will be subject to the illegal immigration laws.
However, if the overstay is unintended and unavoidable, for example you had an accident and are medically not allowed to fly, then the authorities will be more accommodating, although you should contact your embassy as soon as possible to get advice on your situation. Other instances where an overstay could be out of your control could be if flights were cancelled for a period of time for any reason.
You may face issues in the future if you want to apply for another ESTA or a US visa as if you are judged to have misused your ESTA then the authorities may decline future applications.

What if I want to stay in the US for longer than 90 days?

You can apply for a visitor visa if you are intending on visiting for a period that will be longer than 90 days. To apply for the visitor visa, you will need to send an application and attend an interview at the US Embassy in your country to determine whether your visa will be granted.
There are a number of other types of visa that you may also be eligible to apply for depending on the purpose of your visit. If you are visiting the US with the intention of working or studying in the US then there are relevant visas that you can apply for that will enable you to stay for longer than 90 days.
You can find more information about the different types of visa that are available and the process of how to apply for them here.
Do I need to apply for another ESTA if my ESTA expires during stay?
You will not need to apply for ESTA again before you leave the U.S., as the ESTA is providing authorization for you to enter the US and is not required when you exit the US. When you go through the US customs border, your passport will be checked and your ESTA information is electronically linked to it. The border patrol agents will check that you have a valid ESTA to enter the US but no such check is performed going the other way.
Whilst you do not need to apply for a new ESTA if your current one expires whilst in US, 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.
How do I renew my ESTA?
When you return from the US you can apply for a new ESTA at any time. There is no way to renew your ESTA, in terms of extending your current one, you simply apply for a completely new ESTA using the same application process as previously. If you are not planning to visit the US for some time then it may be better to wait until closer to your next planned trip, to ensure that you have plenty of time left on your ESTA validity.