Why isn’t my ESTA valid for 2 years?

If you have obtained your ESTA authorisation, but your passport is due to expire before the end of the 2-year validity period, then your ESTA will expire automatically with the expiration of your passport. So, once you get your new passport, you will need to apply for your new ESTA, and repay the fee. If you gave your email address when you applied for ESTA, you will receive a notice when your ESTA is going to expire, which will advise you to go to reapply.
Another possible reason why your ESTA might expire before the 2 years are up is because you overstayed your 90-day period limit that is permitted under the Visa Waiver Program (VWP) on a trip to the U.S. Even though the travel authorisation is valid for 2 years, you cannot stay in the U.S. for more than 90 days at a time. If you overstay, even if it is just for a couple of days, then you will no longer be able to travel using the VWP. It will also be more difficult to visit the U.S. again using the B2 visa, as an individual who overstays their visa is seen to be more of an ‘unlawful’ person than one who stays within the allocated time frame.
If you overstay your visa for a significant amount of time you may be barred from entering the U.S. completely, for a period of either 3 or 10 years, depending on the length of time that you have overstayed the original 90 days. Another reason your ESTA may no longer be valid is if you have visited the U.S. under the ESTA authorisation and carried out activities that are not permitted by the authorisation, such as getting a job as ESTA does not cover being employed in the U.S.