The focal length is the distance between a hypothetical single-element lens and the sensor required to reproduce a given magnification of image. This is unrelated to the distance from the sensor to the flange. See this simplified example.
If we only used lenses with a single element, the focal length stated would always equal the distance between that lens element and the sensor.
Thus, a lens with a 600mm focal length would need to extend about two feet in front of the camera body. And a lens with an 18mm focal length would need its lens element positioned only 18mm from the sensor. Clearly in an SLR camera this is impossible, as it would extend too far inside the camera.
Shifting the optical centre
In reality, complex lenses have multiple elements and this has the ability to shift the optical centre of the lens assembly.
With a single-element lens, the optical centre is simply where that lens element is, but with more elements working against each other, you can shift that optical centre.
Retrofocus (wide angle) lens
For example, by simply adding one more lens element, you can create a wide-angle lens with a focal length smaller than the distance between the sensor and any of its elements. This is a retrofocusing lens. This allows for smaller focal lengths than the flange-focal-distance length without the back of the lens protruding back into the camera body.
In the same way, but the opposite way around, a telephoto lens is capable of having a focal length that is longer than the actual length of the lens. In these multi-element lenses, the optical centre of the lens is outside the front of the lens assembly.
Therefore, a 600mm telephoto lens doesn't actually have to be 600mm long.
In this diagram f' represents the focal length, which as you can see is longer than the physical length of the lens assembly.
Interestingly, this concept of extending the focal length can be taken to an even greater extreme with a mirror lens, where mirrors further extend the travel distance of the light and hence the focal length without actually increasing the physical size of the lens.