Agglomerative clustering starts with every observation in its own cluster and repeatedly merges the two closest clusters. What "closest" means for clusters is the linkage: the smallest pairwise distance (single), the largest (complete), the average (average), the increase in the within-cluster sum of squares (Ward) or the distance between centroids. All five are updated by the Lance-Williams recursion, which needs only the old cluster-to-cluster distances. The tree records each merge at its height; cutting it horizontally gives k clusters.
Ward's method is hclust(method = "ward.D2"): it works on squared distances and reports the square root of twice the increase in the within-cluster sum of squares. Centroid linkage is run on squared Euclidean distances, as hclust expects, and its heights are shown as distances between centroids (square roots). For the T-cell data the right panel shows the distance matrix with rows and columns in the order of the tree's leaves.
code/ch05_clustering.R (Figure 5.25).