If the xylem of a woody plant is composed of dead tissue, how does sapwood become heartwood?
I'm no expert, but I liked this question and did a quick literature search:

Xylem cells are certainly dead at maturity, and it is these cells that make up the majority of what we call wood. However, wood also contains other tissue types, some of which have live cells at maturity. From what I can glean here and here, it is ray parenchyma tissue that is responsible for many of the changes that take place during the transition from sapwood to heartwood. Ray parenchyma is formed at the vascular cambium, along with xylem, during stem secondary growth (wood formation). Ray parenchyma cells remain alive for years after the xylem cells in the sapwood die. Their functions are transporting nutrients radially in the stem, and storage of carbohydrates. 

As the stem grows in diameter, there is a zone of transition from sapwood to heartwood where the ray parenchyma tissue begins to die. Before it dies, the cells undergo some metabolic changes and begin to synthesize secondary phenolic compounds (many different kinds) from stored starch or sugars. Then, as ray parenchyma cells die, they dump these phenolics into the surrounding xylem tissue, and the accumulation of these durable, decay-resistant compounds in the xylem leads to the formation of heartwood. In many hardwood trees this parenchyma death and phenolic accumulation is preceded by vessel-plugging, in which the ray parenchyma cells form balloon-like growths that block the flow of water through the xylem. So, once sapwood changes to heartwood there should be no living cells left, and the water transport capacity of its xylem is gone or greatly diminished.

There appears to be quite a bit of variability between species in when heartwood formation occurs and what triggers it. From what I could see in my brief literature search, it is all under fairly complex genetic and hormonal control that remains an area of active research. I would suggest (and speculate a bit) that the trigger has something to do with maintaining an optimum amount of sapwood as the tree grows in leaf area (which increases demand for water transport) and height (which increases demand for support, or stem diameter).