Permineralization: the first step to becoming a fossil
The first step towards petrifaction begins when the log or branch or twig is buried in mud very soon after it falls, before the quick decay caused by exposure to air and bacteria can take hold. Mineral-rich groundwater soaks into the log, filling empty spaces in the cells. A weak acid generated by the wood as it begins to break down causes the minerals to precipitate out of solution*, filling these spaces with crystals. This part of the process is called permineralization, which can occur in a blink of the eye, geologically speaking -- less than a hundred years -- though it may take much longer.
The replacement process
As the wood itself disintegrates over many thousands of years, the second step of the process, known as replacement, occurs as minerals take the place of the original tissues, sometimes molecule by molecule. Because some portions of the wood -- such as the layers of the annual rings that grew in the spring -- are more vulnerable to decay than harder portions, they tend to be replaced by minerals first. This results in the preservation in stone of the distinctive concentric ring pattern you see in a freshly cut stump.
Mineral makeup and coloring
The final composition of petrified wood — some form of quartz or calcite, commonly — will depend on what minerals are in the water. A silica solution leads to an often vividly colorful agate, jasper, and/or opal specimen, as these are all varieties of quartz. This is generally the result of the original wood having been buried in mud containing volcanic ash. Excellent examples of this kind of petrified wood are found in the Petrified Forest State Park in Arizona. The different colors are the result of trace minerals in the solution. Copper, cobalt, and chromium give a green or blue color; iron creates red, brown, and yellow; and carbon makes the rock black. A calcified specimen will probably be white or gray when first exposed to the air, and results from a calcium carbonate solution. Sometimes a particular specimen will combine a quartz mineral, such as agate, with calcite, indicating that both silica and calcium carbonate were present in the groundwater solution, though perhaps not at the same time.
Creating a cast
When a piece of wood rots away completely within its mud casing before petrifaction can take place, it leaves a mold behind. If this mold is then filled with crystallized minerals in a large-scale version of permineralization, a cast is created. The cast may preserve all the exterior details of bark and branch stubs but will show no annual rings or cellular structure. Paleontologists can still learn much about a particular tree's environment -- insect damage and the like -- from such casts. They also often make dramatic display specimens.
*Think of salt crystals forming as seawater evaporates. Although the cause of the precipitation is different in that case, the results are similar.