I'm an American. If I travel to the UK for the first time as a tourist and my passport expires while i am still visiting, what would happen?
The US State Department and the UK Government say that your passport should be valid for the entire duration of your stay. Before you fly to the UK, your airline will check your travel documents and will see a message saying that "- Passports and other documents accepted for entry must be valid for the period of intended stay."
As such, you might not get to have this problem, because you run a high risk of the airline denying you boarding. If you do make it to the UK border, you run a risk of being refused entry or having your authorized period of stay limited to the validity duration of your passport. These are all good ways to ruin your vacation.
If none of that happens and you do find yourself in the UK while your passport expires, you'd want to contact the US Embassy or Consulate so that they can get you new travel documents; airlines generally won't let you fly home with an expired passport.
If there's any doubt that your passport could expire during your trip, renew it before you leave (this can be done quite quickly if your trip is imminent). Note that many countries require your passport be valid for 3-6 months beyond the period of your intended stay, so there are a lot of places you could be denied entry if your passport expires soon.