Dendritic spines are thought to grow and recede under LTP and LTD, respectively. See (Bosch and Hayoshi 2011) for a review.
From there, much of the synaptogenesis occurs due to surface molecules present both on the dendrite and the presynaptic axon in the growth cone. Localization and guidance are achieved through gradients of growth factors in the developing nervous system See (Kolodkin and Tessier-Lavigne 2011) for a review of all of these mechanics.
How this maps back onto the human CNS and thinking/learning/memorizing is still up for debate, but some of these mechanisms must have been preserved in higher species.
Bosch M, Hayashi Y. (2012) Structural plasticity of dendritic spines. Curr Opin Neurobiol.,22(3):383-8. (Epub 2011 Sep 28).
Kolodkin AL, Tessier-Lavigne M. (2011). Mechanisms and molecules of neuronal wiring: a primer. Cold Spring Harb Perspect Biol., 3(6). [DOI]