There are membrane proteins that act as structural components of the gap (i.e. the synapses aren't just floating there, they are anchored to each other by via membrane proteins).
https://en.wikipedia.org/wiki/Neuroligin https://en.wikipedia.org/wiki/Neurexin
The most common examples are neurexin (expressed on the pre-synaptic terminal) and neuroligin (expressed on the post-synaptic terminal). Not surprisingly, these proteins are involved in synapse formation in the developping brain, as well as the adult brain (in plastic processes where new synapses are formed). To a first approximation, as synapses are growing, their membrane proteins bind their partners and become anchored. This also leads to intracellular signalling and further maturation of the synapse. That's how synapses know where to attach.