The idea here is simple. We want to be able to use an object in our scene (layer, null, etc.) to control the focus distance of our camera. Let's assume that we have enabled the camera's depth of field and have set the aperture to a value that will cause objects not in the focal plane to be out of focus. We want to be able to move our target object around in the scene and have the camera always focused on it.

At first blush, it might seem that the way to do this would just be to calculate the distance between the camera and the target object and use that as the focus distance. We could use an expression like this (which is even set up to handle the possibility that the camera or the target is a child layer):

target = thisComp.layer("target");
length(target.toWorld(target.anchorPoint) - toWorld([0,0,0]))

Unfortunately, that doesn't quite do it, as you can see in the upper movie to the right. This would work, as long as the target object stays on the camera's z axis. The reason it doesn't work for off-axis targets is because cameras have a focal plane, not a focal sphere. You can see the problem in the illustration below:

Using the top view of our comp, we can see that when the target layer moves off the camera's z axis, it is no longer in the focal plane. So what we need is an expression that calculates the projection of the distance from the camera to the target onto the camera's z axis.

OK - so how do we perform such a calculation? We could do it with a little trigonometry, but fortunately, After Effects has a built-in vector operation, dot(), that will take care of the dirty work for us. We just have to feed it two vectors - the vector from the camera to the target and a unit vector (vector of length = 1) that's pointing down the camera's z axis.

When we apply the result of this calculation to the camera's focus distance, the target layer will always be in the focal plane. You can see the improved result in the lower movie on the right. The illustration below again shows the top view of our comp, this time with the focal plane at the correct distance.

Let's take a look at this magical expression:

target = thisComp.layer("target");
v1 = target.toWorld(target.anchorPoint) - toWorld([0,0,0]);
v2 = toWorldVec([0,0,1]);

Using the distance from the camera to the target to establish the focal distance causes layer to be out of focus when off-axis.

Using the projection of the distance from the camera to the target layer onto camera's z axis keeps the layer in the focal plane.

Nerd Note - the vector operator dot() is no one-trick pony. Besides this fascinating behavior of calculating the projection of one vector onto another, it can also be used to find the angle between two vectors. In math and physics, it is known as the vector dot product, and is worth looking up on the internet if you want to know more.