If you stay at a hotel near the airport, then you need to exit the airport. That means that you must ensure that you can legally pass border control.
From the link for the Gov.UK website @Karlson provided,
You’ll need a visa to pass through the UK in transit
You should apply for a Visitor in Transit visa if you arrive on a flight and will pass
through immigration control before you leave the UK.
Transiting without a visa
You might be eligible for the ‘transit without visa concession’ if:
- you arrive and depart by air
- have a confirmed onward flight which leaves within 24 hours
- have the right documents for your destination (eg a visa for that country)
One of the following must also apply:
- you’re travelling to Australia, Canada, New Zealand or the USA and have a valid visa for that country
As you might expect the transit with visa concessions is entirely up to the Immigration official, so if you want to be absolutely sure, get a Land side transit visa.
The ‘transit without visa concession’ is decided by the immigration officer at the border. You won’t be allowed to transit if they decide you need a visa, so you might want to apply for a transit visa before you travel.