How does this generation of a counter-force work?
Let's assume your hand moves the pen's tip towards the apple's surface with a certain force. In the diagram here, the pen is shown by the red tadpole symbol approaching from the right. The computer constantly receives the tip's position from the phantom and calculates if the tip is in contact with the surface (the gray horizontal line in the diagram). We assume that anything to the left of the grey line is inside the apple. If there is no intersection - nothing happens, the tip is unopposed to proceed towards the surface.
Once the tip makes contact, the computer detects this intersection and gets ready to calculate a counter force. However, at first your tip will actually overshoot a bit! The amount of counter force is dependent on how much the tip is inside the apple, the deeper inside, the stronger the force. The force's direction is calculated so that it will push the tip towards the outside, here this means simple pushing it to the right. The computer will then make the phantom to apply this force (vector), push the tip back a bit, again report the tip's new position for a new calculation.This way, the tip is pushed closer and closer to the surface until it is outside again. As the user continues to push towards the surface (to the left) this leads to a sort of "tug-of-war" between the user's force and the phantom's counter-force.
You may be wondering how this creates the sensation of a surface? The calculations and the adjustments of the counter-force are done very, very quickly - about 1000 times a second! This very quickly leads to the establishment of a "force equilibrium" around the location of the surface and our hand does simply not notice the constant fore-and-back of the forces because it all happens so fast. Something similar happens when more than ~20 images are displayed to you within a second - you perceive a movie instead a series of images.