It rises if it’s lighter than its surrounding liquid and it overcomes capillary pressures, caused by the pores in the rock.
You can describe all materials by its density property, or specific gravity. Which is weight per volume. Typically measured in g/cm3 or t/m3. Since these numbers translate directly into each other I will only give the numbers without the units. Giving the density in kg/m3 is equally valid but adds some digits.
Since many rocks in this table can have heterogeneous composition it cannot be boiled down to a single number. A range is given instead. Depending on composition, mechanical compaction and chemical compaction these ranges can be surprisingly large.
Gold has an almost 20 times higher specific gravity than water which is exploited in placer mining to separated gold from its surrounding material.
Oil and gas is typically lighter than water. Therefore, within a water environment it will rise to the surface. (I say typically lighter because there are some very heavy oils out there that are actually heavier than water)
But why? In the end it is the same answer to the question why a hot air balloon rises,( or a helium balloon).
The only difference from within a balloon to the outside is the density within. For a hot air balloon it is that hot air is slightly lighter than normal air. Helium is naturally lighter than air.
It comes down to the simple equation of hydrostatic pressure, which describes the pressure of a fluid with density RHO of a column of height h.
If you have different fluids in one column, the total pressure is the sum of the pressures of each corresponding subcolumn.
Differences in pressure means the presence of a force. If all pressures are the same, there will be no active force.
Now let’s look at the pressures of points P1 and P2 in the picture. P1 and P2 are at the same height in all 3 cases. The case with an air balloon is essentially the same situation as if you had no balloon at all. P1 and P2 in those cases will be the same.
Air balloon & no balloon:
Since helium has a lower density than air the resulting pressure will be less. So we have a pressure difference around the balloon’s bottom. The air surrounding the balloon therefore exerts a force on the bottom of the balloon. If the force is bigger than the actual weight of the balloon, it will rise. This is called buoyancy.
By cleverly adjusting the density, big objects like zeppelins can hover or fly.
Oil and gas
The same principle applies to droplets of any fluid. Oil and gas is not different from that.
You know the principle from the bartender who mixes colorful drinks with many different liquors.
If a planet was completely made up of liquids and gases (and no energy source to eliminate things like wind and turbulence) you would get a separation of all different liquids and gasses by its gravity just like the perfect drink you order at a bar (but don’t! Geologists drink beer…).
Once hydrocarbons are generated by the kerogen in the source rock it begins to rise if the permeability (ability to flow) of the surrounding rock is high enough to allow it.
Permeability is mainly goverend by capillary forces. If you take a straw and put it into your drink you will see that the level of the liquid is slightly different than the level of the glas. The diameter of the straw determines the strength of the effect and how far the liquid can rise in the straw. The level can also fall. This depends on the kind of liquid used and the wetness of the surface. We say oil-wet or water-wet depending on the water saturation Sw.
Wetness boils down to the question which liquid of a mixture actually touches the surface and which one is encapsulated in the other.
Now if the buoyancy forces are bigger than the capillary forces it can flow through the rock. If not, it will be retained and the rock appears to be impermeable. Another typical misconception about oil and gas reservoirs is that a seal is some sort of magically impermeable hard boundary like someone put a steel lid over it. No, it always has residual pores that prevent migration through it by capillary forces. Another misconception is that this seal is magically strong and is all you need to keep the oil down. No. In the end, the reason why the oil stays below the seal is the overburden on top which exerts a strong pressure on the seal.
If you put a lot of gas into a reservoir the pressure will rise. A lot. Enough to actually break the seal up. This is called seal breach and can happen for example in scenarios where you uplift a whole basin. The norwegian Barents Sea is a prime example for this. So it is not enough to have this barrier. No, you need something on the other side of the barrier that pushes the barrier down. Think of if like a pot on your stove that starts to boil violently. The lid pops up and it will boil over if you just leave it like that. But if you seal it properly and push the lid down it will stay in place.