The defender must get to a position along the line of the attacker and the ball. By recovering towards the goal and then out, the defender can cutoff the attacker's direct line to goal. As a general rule, my preference is to keep the attacker out of the middle. If the defender has to give up something, then let the attacker move down the touch, even if it is to the goal line. If the ball is closer to the halfway than the goal line, then I would lean towards stopping the penetration, as long as the attacker can be forced horizontal.
Players tend to have two specific problems when recovering.
First their recovery angles are often too narrow. That is, they do not recover towards the attacker too soon. This gives the attacker an angle on goal. Second, as the defender veers towards the attacker, the often overrun their defensive position and the attacker is allowed to cut towards the middle. All too often the defender is still in a running stride on the cutback.