The textbooks represent the synapses that way for didactic reasons. But, in fact, there are several protein complexes that keep the membranes (pre and post synaptic) attached. The most famous are the _puncta adherentia_ junctions.
In these structures, several different proteins, like nectins and cadherins, promove cellular adhesion:
![enter image description here](
(source of the image: <
These molecules are important not only for cellular adhesion, but for synaptic plasticity as well.
Source: Adherens Junctions: from Molecular Mechanisms to Tissue Development and Disease