I have a distant memory, over 15 years ago, of reading an old textbook with a more general framework, in which this theorem can be proven in great generality. The following is based on the fragments of what I could recall. I make no guarantee that my nomenclature is standard (nor do guarantee that it's unique!).
Suppose $D \subseteq T \setminus \{\emptyset\}$ has the following properties:
- For any $U_1, U_2 \in D$, there exists some $U_3 \in D$ such that $U_3 \subseteq U_1 \cap U_2$ ($D$ is a downwards-directed set with respect to set inclusion), and
- For any $U \in D$, there exists some $U' \in D$ such that $\operatorname{cl} U' \subseteq U$.
We will call such a set $D$ a direction. We can use this concept to, very generally, state and prove the property you're talking about.
Some examples of directions in $\Bbb{R}$:
- For any $a \in \Bbb{R}$, the set $\{(a - \delta) \cup (a + \delta) : \delta > 0\}$ is a direction, which we denote simply by $a$.
- The sets $\{(a, a + \delta) : \delta > 0\}$ and $\{(a - \delta, a) : \delta > 0\}$ are both directions, denoted by $a^+$ and $a^-$ respectively.
- We also denote the directions $\{(x, \infty) : x \in \Bbb{R}\}$ and $\{(-\infty, x) : x \in \Bbb{R}\}$ by $\infty$ and $-\infty$ respectively.
- The set $\{(-\infty, x) \cup (x, \infty) : x > 0\}$ is also a direction, which we could notate $\pm \infty$.
These encapsulate the idea of $x \to a$, $x \to a^+$, $x \to a^-$, $x \to \infty$, $x \to -\infty$, and $|x| \to \infty$ respectively.
We can define limits with directions too. Specifically, rather than saying $x$ approaches a number, we will say $x$ approaches a direction. The limit too will be a direction. We aim to have $\lim_{x \to a} f(x) = L$ mean the same thing as it normally would, even if $a$ is replaced by $a^+, a^-, \infty, -\infty$, etc. But, this is more flexible: with one definition, we define one-sided limits and infinite limits, both in terms of the dependent variable $x$ and the independent variable $f(x)$.
Suppose we have topological spaces $(X_1, T_1)$ and $(X_2, T_2)$ (again, feel free to sub in $\Bbb{R}$ to both), and $D_i$ is a direction in $(X_i, T_i)$ for $i = 1, 2$. Further, suppose $f : X_1 \to X_2$. Then we say
$$\lim_{x \to D_1} f(x) = D_2$$
if, for all $\mathcal{V} \in D_2$, there exists some $\mathcal{U} \in D_1$ such that
$$x \in \mathcal{U} \implies f(x) \in \operatorname{cl} \mathcal{V}.$$
I hope it's clear that, in the case of $\Bbb{R}$, making $D_1 = a$ and $D_2 = L$, we get the usual definition of $\lim_{x \to a} f(x) = L$. With some work, you could verify that all the various definitions possible are all faithfully encapsulated with this framework.
With that in mind, we can prove the following general theorem:
Theorem. Suppose $(X_i, T_i)$ is a topological space, and $D_i$ is a direction in that space, for $i = 1, 2, 3$. Further, suppose $g : X_1 \to X_2$ and $f : X_2 \to X_3$. Then,
$$\lim_{x \to D_1} g(x) = D_2 \text{ and } \lim_{x \to D_2} f(x) = D_3 \implies \lim_{x \to D_1} f(g(x)) = D_3.$$
Proof. Suppose $\mathcal{W} \in D_3$. Since $\lim_{x \to D_2} f(x) = D_3$, there exists some $\mathcal{V} \in D_2$ such that
$$y \in \mathcal{V} \implies f(y) \in \operatorname{cl} \mathcal{W}.$$
By the second property of directions, there must exist some $\mathcal{V}' \in D_2$ whose closure is contained in $\mathcal{V}$. Using the fact that $\lim_{x \to D_1} g(x) = D_2$, there exists some $\mathcal{U} \in D_1$ such that
$$x \in \mathcal{U} \implies g(x) \in \operatorname{cl}\mathcal{V}' \subseteq \mathcal{V} \implies f(g(x)) \in \operatorname{cl}\mathcal{W}.$$
Thus, by definition, $\lim_{x \to D_1} f(g(x)) = D_3$. $\square$
And that's that! Now, a couple of closing remarks:
Directions can be compared with the relation
$$D_1 \le D_2 \iff \forall U_1 \in D_1, \exists U_2 \in D_2 : U_1 \subseteq U_2.$$
In this way, we see that $a^+, a^- \le a$, or $\infty, -\infty \le \pm \infty$. Moreover, we get nice results like, if $\lim_{x \to D_1} f(x) = D_2$ and $D' \le D_1$, then $\lim_{x \to D'} f(x) = D_2$. Or, if $D' \ge D_2$, then $\lim_{x \to D_1} f(x) = D'$.
This reveals something important about limits with directions: they are (mostly) not unique! Indeed, using the $\lim_{x \to D_1} f(x) = D_2$ notation is already problematic, as the left hand side does not refer to any one direction. It would probably be better to say $f(x) \to D_2$ as $x \to D_1$ instead.
The $\le$ relation is a pre-order, i.e. is reflexive and transitive, but not necessarily anti-symmetric. The symmetrised relation,
$$D_1 \sim D_2 \iff D_1 \le D_2 \text{ and } D_2 \le D_1$$
is an equivalence relation, and indeed, it would be fair to consider such directions to be "equivalent". It would mean that one could swap out one direction for an equivalent one in a limit at will. We could, for example, equivalently define the direction $a$ (where $a \in \Bbb{R}$) by $\{(a - 1/n, a + 1/n) : n \in \Bbb{N}\}$ instead, and we would obtain an equivalent direction.
Be aware that the above definition of a limit assumes a "full" domain, i.e. $g$ is defined everywhere on $X_1$ and $f$ is defined everywhere on $X_2$. This may seem restrictive at first, but remember that we could define our topological space to be a subset of $\Bbb{R}$, and equip it with the subspace topology. If we have $Y \subseteq X$, and $D$ is a direction in $X$, then $D_Y = \{U \cap Y : U \in D\}$ is a direction provided $\emptyset \notin D_Y$. This takes care of real functions whose domain is not the entirety of $\Bbb{R}$.