Oil and gas, being less dense than water, "wants" to migrate upward through the rock strata from the source rock (which is almost always shale) where it was generated. Occasionally it will encounter geologic features, such as a salt dome, which trap it. The hydrocarbons get trapped because they can't move downward, they can only move up. They can't move downward because everything else around it (rock and water) has higher density.
The other poster who alluded to the density of the anhydrite is sort of right, but for the wrong reason. The salt has very low density (relatively) and thus it also wants to move upward through the rock strata, and as it moves upward it also expands due to there being less stress on it. This gives it the geometry that it needs to form an effective trap for oil and gas.
Conventional oil explorationists also look for anticlines (think upside down bowls), faults, and stratigraphic pinchouts which can also trap hydrocarbons.