The tubing, pump and sucker rods are all inside the casing). When the rods are traveling up, the traveling valve is closed and the standing valve is open (due to the drop in pressure in the pump barrel). Consequently, the pump barrel fills with the fluid from the formation as the traveling piston lifts the previous contents of the barrel upwards.
When the rods begin pushing down, the traveling valve opens and the standing valve closes (due to an increase in pressure in the pump barrel). The fluid in the barrel (which was sucked in during the upstroke) flows up through the traveling valve. The piston then reaches the end of its stroke and begins its path upwards again, repeating the process.
Often, gas is produced through the same perforations as the oil. This can be problematic if gas enters the pump, this can result in "gas locking", where insufficient pressure builds up in the pump barrel to open the valves (due to compression of the gas) and little or nothing is pumped. To avoid this, the inlet for the pump is placed below the perforations. When the gas enters the well bore through the perforations, it bubbles up the annulus (the space between the casing and the tubing) and does not have space to make it into the pump. Once at the surface, the gas is collected through a tube connected to the annulus.