The lens has a mainboard and the camera body has a mainboard. Mainboards consist of processors (CPUs), inputs/outputs and control lines.
The lens will have data of the lens in it, such as min/max aperture.
The lens and body communicate to each other to auto-focus, set the aperture and determine the focal length (at a rough guide).
For the focal length, generally the lens has a set of contact brushes which slide across contacts on the mainboard. At different points it will indicate the focal length and the lens will communicate this to the body when the photo is taken.
When it's auto-focussing, it needs as much light as possible. Generally if it's a f1.8 lens and you set it to f5.6, the body will set it to f1.8 during the auto-focus process and then once it's in focus, the body will set it to f5.6 and take the photo.
Of course, as has been stated, things change when your not using proprietary products.