There's no 'tricks' to not run out of gas.
First of all to make sure you're making the most of your gas, at each fully saturated base there should be 3 workers at each refinery. With three workers there's a small delay before the next worker can go into the refinery so putting any more workers will not improve your income.
That being said, having two workers gives more gas per worker than having three. The reason for this is that there is no delay, so your workers are never just sitting around doing nothing, they're always being productive.
When you expand, if you're doing a build/composition heavy on gas, you'll want to start building your refineries before your CC finishes, so that as soon as you're expansion is complete you can fully utilize the gas there. To further improve gas efficiency in this way you can send two more workers to each refinery before it even completes. They will start mining once it's done without you having to do anything. This lets you forget about it and worry about other things.
Furthermore, if it's just gas you want, you can even long distance mine it. So just build the refineries at your expansion (without a cc there) and then send spare scvs to mine it. Use this method with caution as the efficiency goes right down. That being said it's still way more efficient to long distance mine than having over 30 scvs at a single base.
Now we have to look at why you're running out of gas. Are you trying to do too much at once? For instance trying to get thors and ghosts and tanks and marauders and medivacs, as well as double upgrades and other research is probably too much all at once. Try and aim for three production buildings and one piece of tech per expansion.
Focus on a single tech path, like marine, marauder and medivac. In this composition, the medivacs are gas expensive but marines and marauders are quite cheap on gas. So with this sort of composition you won't run out of gas. If you're going lots of thors, say from two or more factories, you probably will need to focus heavily on marines and not much else, maybe a few medivacs to heal stims.
This is a training drill that's fairly common for learning how to macro well. It simplifies macro by eliminating most of the options available to the player, which lets them focus more on macro instead of other things, like what units to build.
With this drill you do not get any gas at all. This means you can only build CC's (OC), scvs, barracks, marines and supply depots. By only having these options it should be easier to focus on macro.
After practicing with only minerals for a while, then you incorporate more and more units, start with marine upgrades, then add marauders, then medivacs, etc. Add a unit every few games until you've practiced up to having all units available.
But how does this help "running out of gas"? I think that by practicing in this way, you will gain a better understanding of what kind of composition is good to have, you should garner a better appreciation of lower tier units. I think that you may be focused on the higher tier "stronger" units and aiming to get more of them. But it is the lower tier units that should be the core of your army, and then you supplement them with few high tier units.