The question was how, not why.
Your answer is like saying "how does the eye focus light" and answering "so that you can see".
That still doesn't describe the underlying mechanism of the growth itself.
Looking at the work of Dr. Michael Levin regarding electric communication of cells, I tend to believe him that the main factor in actually creating tissues in their intended, correct shape, is incessant electric chatter among individual cells.
An interesting corollary would be that cancer = cells that don't cooperate/communicate with their neighboring cells anymore.
Where does the rotational sense of cilia come from? From the stereochemistry of proteins, and therefore from amino acids. The left-vs-right handedness of the base chemistry of life is exploited to get a macroscopic signal.
In plants, for example, cells replicate primarily at the tip of a bud, which allows branches to lengthen directionally rather than grow out in all directions. The plant produces growth hormones, which are transported upward throughout all branches until they reach a dead end. When they reach a dead end, they stop moving and just sit there, which causes the cells at the dead end to have a greater exposure to these growth hormones. These cells bathe in growth hormones for so long that they pass the hormone-exposure threshold that triggers cell replication.
For example, observation A might be maladaptive, but it is caused by gene B which also causes observation C, which does provide an advantage.
Nails are a much older "invention of evolution" than humans: so we have to investigate there...
> how do nails and hairs manage to grow only in one direction
Like all things evolution: nails that grow backwards to not have an advantage (prolly a disadvantage), where nails that grow forwards have an advantage (climbing, clawing, scratching).