Start with a simple converging lens. The lens is often indicated by a vertical line with triangles on the top and bottom, but this is shorthand for the dashed concave lens shown below. Assume this is the objective lens of your microscope. A lens has two focal points. Light originating at the left focal point exits the lens horizontally (yellow), like in a searchlight. Light coming from a distant object (purple) converges at the right focal point, like in a telescope.
When an object is not so distant, the light converges at a point beyond the focal point; the closer the object, the farther back it converges. You can calculate the point where the light converges using the thin lens equation (Eq. 14.64 in IPMB). Below I show three rays originating at different positions in a sample of biological tissue. The colors (green, blue, and red) do not indicate different wavelengths of light; I use different colors so the rays are easier to follow. Light originating deep in the sample (green) converges just beyond the right focal point, but light originating near the front of the sample (red) converges far beyond the focal point. This is why in a camera you can adjust the focus by changing the distance from the lens to the detector.
Suppose you wanted to detect light from only the center of the sample. You could put an opaque screen containing a small pinhole beyond the focal point of the lens, just where the blue rays converge. All the light originating from the center of the sample would pass through the pinhole. The light from deep in the sample (green) would be out of focus, so most of this light would be blocked by the screen. Likewise, light from the front of the sample (red) is even more out of focus, and only a tiny bit passes through the pinhole. So, voilá, the light detected beyond the pinhole is almost entirely from the center of the sample.
Do you want to view a different depth? Just move the screen/pinhole to the right or left, and you can image shallower or deeper positions.
How do you get information in the plane at one depth? In confocal microscopy, you usually scan a laser at different positions in the x,y plane (taking z as the distance from the lens). Pixel by pixel, you build up an image, then adjust the position of the screen and build up another image, and then another and another.
Often, confocal microscopy is used with samples that emit fluoresced light. You shine the narrow laser beam of short-wavelength light onto the sample from the left. The sample then emits long-wavelength light as molecules in the sample fluoresce. You can filter out the short-wavelength light, and just image the long-wavelength light. Biologists play all kinds of tricks with fluorescence, such as attaching a fluorescent molecule to the particular structure in the sample they want to image.
There are advantages and disadvantages of a confocal microscope. On advantage is that your detector, positioned to the right of the screen/pinhole, need not be an array like in a camera. A single detector is sufficient; you build up the spatial information by scanning the laser beam (x,y) and the pinhole (z) to obtain full three-dimensional information that you can then manipulate with a computer to create informative and beautiful pictures. One disadvantage is that you have to scan both the laser and the pinhole in synchrony, which is not easy. All this scanning takes time, although video-rate scans are possible using modern technology. Also, most of your fluoresced light gets blocked by the screen, so you need an intense light source that may bleach of your fluorescent tag.
The confocal microscope was invented by Marvin Minsky, who died last year after a productive career in science. Minsky was an undergraduate physics major who went on to study mathematics, computer science, artificial intelligence, robotics, and consciousness. Isaac Asimov claimed in his biography In Joy Still Felt that only two people he knew were more intelligent than he was: Carl Sagan and Minsky. Marvin Minsky and his confocal microscope illustrate the critical role of physics in medicine and biology.