To perform both sets of tasks you must make sure that both the distance between you and your opponent and your defensive stance are correct. Arm's length is the normal distance between the defender and the player with the ball. Closer, if the defender is quicker. Farther, if the defender is slower, but not so far as to not to be able to challenge a shot.
Denying A Driving Lane
Even before your opponent receives the pass, you should have decided which driving lane you are going to deny or protect. Overplay to that side. When the ball is in a forward position, many teams will, for example, protect the middle. Consequently, when an opponent has the ball in the right forward position, the defender will overplay to his/her right and to the opponent's left.
In a normal overplay, your head is opposite the shoulder of your opponent, when he/she is square to the basket. Depending on a number of factors, such as the relative quickness of you and your opponent and of his/her skill level, you can vary the amount of overplay.
Until your opponent shoots, passes, or drives, adjust your defensive position by either forward or backward popping and by adjusting the location of your hands. For example, when your opponent initiates a drive, execute a backward pop to create some space. When he/she reacts by initiating a shot, close by executing a forward pop. Throughout, maintain your overplay position.
Guarding the Drive
A drive begins the moment the ball leaves the players hands. At that moment, should the player drive away from the overplay, simple play normal defense. See my tip Guarding the Dribbler.
Should the player drive to the side of the overplay, reverse pivot on the rear foot, so that you face him/her, and play normal defense.