From the viewpoint of formal logic, this is called the "substitution" property of equality. We begin with axioms of identity that say that, for any expression $E$ (which is well defined) we have $E = E$.
We then have axioms of substitution. If we have an expression $E = E$ and we already know that $A = B$, then we can replace any number of copies of $A$ within $E$ with $B$, as long as we preserve the proper structure.
So, for example, we would have
$$
f(x) + h(x) = f(x) + h(x)
$$
as a basic axiom. If we also know that $f(x) = g(x)$ then we can replace - substitute - one of the copies of $f(x)$ in the first equation with $g(x)$ to obtain
$$
f(x) + h(x) = g(x) + h(x).
$$
Of course, if these are functions, we may need to pay attention to make sure that $f(x)$, $g(x)$ and $h(x)$ are all defined.
Note that, when we work with these axioms, we do not "start with $f(x) = g(x)$ and add $h(x)$ to both sides". Instead we start with a longer equation that we already know is true, and we make a substitution of one piece by another piece that is equal to it.
In this way, the identity and substitution axioms allow us to manipulate equalities within formal logic in the way that we expect. This is exactly why we include these axioms: because we know how equality "should" work, and both of these axioms are motivated by that understanding.