Do hotels in the United States in New York City for example have to uphold code requirements for living space?
If they don't couldn't someone just open up a couple of sheds in their backyard call it a hotel and rent them out?
This question is controlled almost entirely by local municipal and county ordinances. It is not a question of federal law that is uniform across the U.S. and in most cases it is not even a question of state law. So, there is no single answer to your question.
Most localities regulate this with some combination of hotel specific building codes (usually incorporated by reference from a uniform building code promulgated by a private non-profit organization as a model building code provision) and hotel specific local zoning ordinances.
Hotels are regulated, my neighbor's house is not. A 'hotel' is defined in RCW 19.48.010:
Any building held out to the public to be an inn, hotel or public lodging house or place where sleeping accommodations, whether with or without meals, or the facilities for preparing the same, are furnished for hire to transient guests, in which three or more rooms are used for the accommodation of such guests, shall for the purposes of this chapter and chapter 60.64 RCW, or any amendment thereof, only, be defined to be a hotel, and whenever the word hotel shall occur in this chapter and chapter 60.64 RCW, or any amendment thereof, it shall be construed to mean a hotel as herein described.
My neighbor does not call it a hotel, and there is only a single room occasionally rented out as a sleeping accommodation.
RCW Chapter 79.62 addresses safety laws, this time referring to "transient accommodation" which means
any facility such as a hotel, motel, condominium, resort, or any other facility or place offering three or more lodging units to travelers and transient guests
and note that it doesn't have to be a building. Also, this definition does not include "for hire". Most of the implementational details are spelled out in the regulations administratively written to say exactly what one must do, in 246-260 WAC, which covers "facilities offering three or more lodging units to guests for periods of less than thirty days", including "rustic resorts" and B&Bs. As you can see, it depends on how many sheds or how big a shed you have.
Rustic resorts typically fail to be "up to code", in fact many buildings are not up to current code. "Code" is a concept that applies to construction: you can be up to code in 1947 and totally out of code 60 years later. There can, then, be specific requirements of particular establishments – hotels – regarding safety and sanitation. 246-260 WAC says what you have to do in that respect.
The government has regulatory discretion to waive requirements if you can devise a decent argument for doing so. An example is that any such facility has to have electricity, water, sewer etc. and that is an unreasonable requirement for a cabin in the mountains that you hike to. It is hard to predict when they will consider exemption to be reasonable vs. unreasonable, but as far as an urban tool shed is concerned, it would be unreasonable to suspend the health and safety regulations.
Before you decide to start renting your tool shed out, you should also consult municipal ordinances. Seattle also regulates businesses and has ordinances related to operating B&Bs, which were recently made more draconian to cut back on the number of B&Bs. The ordinance uses the expression "dwelling units or portions thereof", and does not specify a threshold of three as per state law. The trigger is being "occupied by a person for less than 30 days". Therefore, in Seattle, your shed-motel is regulated.