The rules are laid out in the US Constitution.
The president can temporarily turn their duties over to the vice president while they are unable to do the job. This has happened when past presidents were under anesthesia for minor procedures. The president takes back their powers as soon as they are awake.
If the president is suddenly incapacitated, if he has a stroke, for example, the vice president can assume power if a majority of the cabinet agrees.
The Presidential Succession Act of 1947 says that if both the president and the vice president are unable to serve, the Speaker of the House becomes the president.