The cephalopods are part of the class of invertebrates that include octopuses, squids, and nautiluses, all grouped under the phylum of mollusk. The speciation of cuttlefish and other cephalopods is quite complex, as can be seen in this Encyclopedia of Life link. The common cuttlefish is known as sepia officinalis.
The biological mechanism behind this intricate and fast acting camouflage capability includes a network of more than 20 million specialized cells called chromatophores that contain sacs of pigment. As in Figure 1 below, the chromatophore cells are attached to muscle fibers that connect to the cell body radially and are innervated with nerve axons.
The colors of the chromatophore pigmentation are yellow, red, and brown. There are other cells involved in cuttlefish coloration, but the chromatophores are the most kinetic of the set owing to their nexus with the neuromuscular system.
In the illustration in Figure 2 below, we see on the left a chromatophore cell at rest. On the right we see the cell activated via expansion of the radial muscle fibers. Note how the yellow pigment sac in this instance is stretched out by the strands of muscle, increasing it's surface area sufficiently to render the color visible.
This color palette ensures that the cuttlefish can produce virtually any color in the color spectrum. When you're an extant species belonging to a biological class that originated in the Ordovician era, between 485 and 444 million years ago, there's been much time for evolution to solve certain adaptation problems.
Although the issue is not definitively settled, the current consensus among marine biologists appears to be that cuttlefish process their background environment visually, via their "W" shaped eyes.
That visual sensory input subsequently undergoes highly sophisticated neural processing, sending commands to the radial muscle fibers attached to the chromatophore cell bodies. Specifically, on how to expand in the correct sequence and combinations in order to produce the desired skin color and body shape. The fact that this process is coordinated at so many levels of organization so quickly staggers the imagination.
Obviously the ability to innervate over 20 million cells in order to produce complex colors and shapes, informed by background color/textures cues no less, requires gargantuan neural processing power. It turns out that cuttlefish, along with other cephalopods, have large brains and a high encephalization quotient. This metric is thought to be a predictor of sorts of the intelligence of an animal.
The PBS NOVA video embedded below from 2007 offers a fascinating glimpse into the behavior and biology of this magnificent animal, and is well worth the time.