It depends on the type of glue.
Different types of glue function differently. The most effective ones undergo a chemical reaction with the material they are bonding (the substrate). As a result, new molecules are formed, partially consisting of atoms from the substrate and partially from atoms of the glue.
Many glues adhere to the substrate due to intermolecular attractive forces (Van der Waals forces). These forces arise from the presence of electric poles in the molecules, both permanent and temporary. Molecules are attracted to each other by opposite poles.
Some glues form hydrogen bonds with the substrate. This is a particularly strong type of intermolecular attraction that occurs when an atom of one molecule is attracted to the hydrogen atom of another molecule.
If the substrate dissolves in the glue, the bonding of two objects can occur through diffusion, that is, due to the penetration of molecules from one object into another. For example, plastic parts are joined using a plastic solvent. There are also other forces that can participate in the bonding process.