So the term you want to search for is "Conservative Forces". I'll give a brief explanation here so you can find a better one more easily. :)
Let's take a few steps back and talk about forces, and work. Gravity isn't really a key part of your question.
Suppose you had a uniform downward force field like so:
| | | | | | | |
v v v v v v v v
| | | | | | | |
v v v v v v v v
| | | | | | | |
v v v v v v v v
Those are vectors pointing downwards. Now suppose you built a miniature roller coaster. Your first one is super boring
| | | | | | | |
v v v v v v v v
car
|---\
<-------------------->
| | | | | | | |
v v v v v v v v
It just goes back and forth, perpendicular to the field. Now, if you really grease up the tracks, and have really good bumpers on the end (noiseless, don't heat up, a perfect elastic collision), the car will just bounce back and forth for a very long time, and not speed up or slow down much at all (and ideally, none at all).
There is a concept of 'work' in physics. When a force points in the direction of motion, we say that a force does work. The total work done along a path is simply the sum (integral) of all the parts of the path, multiplied by their length, multiplied by the amount of force in the direction of the path. This definition chosen so that the amount of kinetic energy gained, or lost, is equal to the work done.
Since the force is perpendicular for the above path, the 'work' done is said to be zero.
Now let's have a more interesting path. Let's give the car a kick.
car
<- /---|
/--------<-------\
| |
v ^
| |
\------->--------/
Here we have two parts, top and bottom, where the force does no work. However, we have a part going down, and a part going up. The car will accelerate going down, and decelerate going up. If we grease the tracks up,
then the car will always be traveling at the same speed on the top track. It will also always be moving faster on the bottom track.
We say that the force does 'work' when the car is on the tracks going down and going up.
Now, the total amount of work that the force does, for any loop, ends up being zero. This is clear here, since the distance the force is applied downwards, is the same as the distance of the force applied upwards.
What is slightly less obvious is that any looped track we could build here would have the same property. If we had something like
/---\
| |
\ |
\ |
\|
(and ignore the mechanics of those super sharp turns) we would find that the work done was still zero. The downward diagonal bits would end up still adding up the same amount of work as the upward straight bits (bit of vector math shows this).
The fact that the work done on any loop is zero makes this particular force a 'conservative force'.
Not all force fields need to be conservative! Suppose our field was only
present on the left side.
/---|
/--------------------\
| | | |
v | v |
| |
| | | |
v | v |
| |
| | | |
v | v |
\--------------------/
This would only do work going down. This would mean that the track car would keep getting accelerated faster and faster.
Potential energy only makes sense as a concept in the presence of conservative fields. You define a reference point, sometimes chosen to be at infinity, and define the potential energy at each point to be the amount of work done to bring the object to that test point.
However, you don't need to just be a constant field to be conservative. Gravity, as expressed by Newton, is a conservative field. Regardless of how you arrange a bunch of objects or planets, looped paths will always have zero net work done. This is because the mathematical form of Gravity can be epxressed as the gradient of a scalar (takes a position, gives a number) function.
You seem happy with the idea that locally on earth, energy is conserved because you get the energy back when you travel in a loop. This doesn't change when you travel in a more complicated path from one planet to another. The force field does work on the object to accelerate it when it moves in the direction of the force, and decelerates it when you pull it back.
The big takeaway point here is that energy, as a concept, is built on top of (at least classically) the more primitive concept of 'force'.