There are 2 parts of H-1B - petition and visa stamp. Here is the general process
When the petition (797) expires or is about to expire in next 6 months, a new cap-exempt petition needs to be filed which will give you an extended 797. This needs to happens irrespective of whether you are inside US or not.
If you are inside US, then (1) is accompanied with Extension of Status, which gives you an extended I-94 and allows you to stay and work inside US. Next time you travel outside of US, you will have to get a new visa stamp (see point 3).
If you are outside US, then after (1) you need to appear for visa stamping. Once approved, you will receive a new visa stamp for the extended period.