In a category $Alg$ of algebraic structures, such as groups, the forgetful functor $U:Alg \to Set$ has a left adjoint $F$. Thus given a subset $S \subseteq U(A)$ we have the adjoint map $FS \to A$ and now the substructure $\langle S \rangle$ of $B$ generated by $S$ is obtained by factorising $FS \to A$ through its image as a surjective homomorphism followed by an injective homomorphism $FS \to \langle S \rangle \to A$.
The universal property follows from this construction: namely, given a substructure $T \hookrightarrow A$, then to say that $S \subseteq UT$ as a subobject of $UA$ is, by adjointness, to give a map $FS \to T$ such that the square
$$\begin{matrix}FS\rightarrow&<S>\\\\\downarrow&\downarrow\\\\T\rightarrow&A\end{matrix}$$
commutes. Because $FS \to \langle S \rangle$ is surjective and $T \to A$ injective there exists a unique factorisation $\langle S \rangle \to T$ making both triangle commute. (This relationship between surjections and injections is called orthogonality - see the notion of factorisation system.)
Your theorem can then be established using the orthogonality and composability properties of surjections and injections. A natural generalisation of the above is to a "concrete category" $U:C \to D$ over a general $D$ where $U$ has a left adjoint and where $C$ admits a (strong epi/mono)-factorisation system.