Certainly, if $\lim_{x\to a} g(x)$ exists, and $f$ is continuous at $\lim_{x\to a} g(x)$, then the two expressions exist and are equal.
If $f$ is not continuous, even if both expressions exist, they may not be equal. For example
\begin{align*}
f(x) &= \begin{cases} 0 & \text{if } x \le 0 \\ 1 & \text{if } x > 0 \end{cases} \\
g(x) &= x^2 \\
a&= 0.
\end{align*}
Certainly, if $\lim_{x\to a} g(x)$ exists, and $f$ is continuous at $\lim_{x\to a} g(x)$, then the two expressions exist and are equal.
If $f$ is not continuous, even if both expressions exist, they may not be equal. For example \begin{align*} f(x) &= \begin{cases} 0 & \text{if } x \le 0 \\ 1 & \text{if } x > 0 \end{cases} \\ g(x) &= x^2 \\ a&= 0. \end{align*}