Note that the notation $(f\circ f)(x)$ gives some intuitive notion that we are going to do $2$ different operations. The first will be to "feed" $x$ to $f$, and then "feed" $f(x)$ to $f$. Indeed we have $(f\circ f)(x) = f(f(x))$. We know that $f$ is not defined for $x = -1$ and therefore the inner $f$ in $f(f(x))$ cannot be "fed" $-1$. However, the outer $f$ is fed values from $f(x)$ and it just so happens that $f(x) = -1 \iff x = -\frac12$.
We can then proceed to write the algebraic manipulations you wrote assuming that $1+x \not= 0, \frac{x}{1+x} \not= -1$ given that otherwise you would be dividing by $0$.
On the other hand, starting from $\frac{1}{2x+1}$ one cannot write
$$\frac{\frac{x}{1+x}}{\frac{1+2x}{1+x}}$$
if we don't explicitly state that $1+x\not=0$, otherwise you would be dividing by $0$. Therefore one can always do the algebraic manipulations, given that one carries the excluded points along.
Therefore, one finds $f(f(x))$ to be defined for $x\not\in \{-\frac12, -1\}$ and for the points where it is defined we have $f(f(x)) = \frac{1}{2x+1}$. Similarly, working backwards like you did, we get
$$\frac{\frac{x}{1+x}}{\frac{1+2x}{1+x}} = \frac{\frac{x}{1+x}}{1 + \frac{x}{1+x}}$$
except for the points $x = -\frac12, -1$ because we had to exclude them.
What is more, you are right when you say that two functions are equal if they have the same domain/codomain and if they map the same objects to the same images. Having that in mind, the functions $f(f(x))$ and $\frac1{2x+1}$ are not the same function, unless you restrict the second one to the points where we know $f(f(x))$ is well-defined.
Consider the function $g:\mathbb{R} \times \mathbb{R} \to \mathbb{R}$ by $g(x,y) = x$ and $f:\mathbb{R} \to \mathbb{R} \times \mathbb{R}$ by $f(x) = (x,0)$ then $g \circ f : \mathbb{R} \to \mathbb{R}$ is bijective but $g$ is not injective and $f$ is not surjective. Here the function $g \circ f$ is infact identity and hence bijective!
Best Answer
We need to work with one of two definitions:
Definition 1: Given two functions $f\colon X \to Y$ and $g\colon A \to B$, their composite $h = f\circ g$ is the function $h\colon g^{-1}(X \cap g[A]) \to Y$ given by $h(x) = f(g(x))$.
Definition 2 Given two functions $f\colon X \to Y$ and $g\colon A \to B$ such as that $g[A] \subseteq X$, their composite is the function $h\colon A \to Y$ given by $h(x) = f(g(x))$.
With both definitions, $h$ is well defined. Using definition 1, we can have an empty composite, a function from the empty set, while using definition 2, given that our sets are non-empty, then the composite is non-empty.
Also, when they apply, both definitions agree. For the sake of simplicity (given that we don't want to mess around with empty functions), we usually work with the second, less general definition.
Now, we may find ourselves in the middle way of the two extremes: we don't have $g[A] \subseteq X$, but their intersection is non-empty either: $X \cap g[A] \neq \varnothing$. For instance, take $g\colon \mathbb{R} \to \mathbb{R}$ and $f\colon \mathbb{R} - \{0\} \to \mathbb{R}$ given by $g(x) = x^2 - 1$ and $f(x) = \dfrac{1}{x}$. To define their composite $h$, we usually restrict $g$ to the pre-image of the non-problematic points, so we would be considering as it's domain the set $\mathbb{R} - \{\pm 1\}$ instead of simply $\mathbb{R}$. That's usually implicit when talking about composite functions, because when this happens, this restriction can always be done.