As you note in the comments, part of the issue is that when you perform some kind of transformation $f$ to the equation, you really want that
$$a=b \iff f(a)=f(b)$$
hold; for arbitrary expression, we only get the $\implies$ direction, so that a solution to $f(a)=f(b)$ need not yield a solution to $a=b$.
But whether the $\Longleftarrow$ implication holds may depend on the domain of interest: what kind of values $x$ is allowed to take. (Because restricting the values may turn a function that is not invertible/one-to-one into one that is). For example, if you were working exclusively in positive real numbers, then you would get no solutions at all in the second equation, either before or after you multiply by $x$. If you are only interested in real solutions, you can multiply both sides of an equation by $x^2+1$ and that will never produce extraneous solutions, because $x^2+1$ is never equal to $0$. But if you were looking for solution in $\mathbb{C}$, that expression would introduce potential extraneous solutions.
I'm assuming you are working in $\mathbb{R}$, the real numbers.
When you multiply by an expression involving $x$, to ensure that you do not change the solutions you need to make sure that expression is not equal to $0$. The way to keep track of this is to either consider the case where the expression is equal to $0$ separately, or check if that case happens to solve the equation.
So for example to ensure that multiplication by $x$ is reversible, you should add the clause "and $x\neq 0$" to your equations, and then consider what happens if $x=0$. Likewise if you divide by, say, $x-1$ (you would be saying "and $x\neq 1$", and need to consider the case $x=1$ separately).
Look at your first equation,
$$ x+\frac{1}{x} = 2.$$
There is already an implicity "$x\neq 0$" in that equation, because the expression on the left is not defined at $x=0$. It's like having a function $f(x)$ defined by $f(x) = x+\frac{1}{x}$, and you want to figure when it takes the value $0$. Because $0$ is not in the domain of $x$, you will never take $x=0$: it's disallowed.
So what you have, with everything explicit, is:
$$\begin{align*}
x+\frac{1}{x}&=2&&\text{and }x\neq 0\text{ because of the }\frac{1}{x}\\
x\left(x+\frac{1}{x}\right) &= x(2) &&\text{and }x\neq 0\text{ because of the }\frac{1}{x}\\
&&&\quad\text{ and because we multiplied through by }x\\
x^2+1 &= 2x &&\text{and }x\neq 0\text{ for multiple reasons.}
\end{align*}$$
So you didn't change the valid domain of your computations when you multiplied everything by $x$, because you had already excluded it when you wrote down your initial equation.
By contrast, in your second equation you do not have that initial exclusion, so when you multiply by $x$ you do change the scope:
$$\begin{align*}
2x+4&= 0\\
x(2x+4) &= x(0) &\text{and }x\neq 0\text{ because we multiplied through by }x\\
x(2x+4) &= 0 &\text{and }x\neq 0\text{ because we multiplied through by }x
\end{align*}$$
Solving this will give only $x=-2$ (because $x=0$ is explicitly excluded). To be thorough, one would now check to see if $x=0$ happens to be a solution to the original equation (because we excluded it for no good reason during our computations). By contrast, in the first equation we don't need to check whether $x=0$ happens to be a solution because it was excluded a priori, and not by our manipulation of the expression.
Is that the reason we get extraneous solutions? @ArturoMagidin
– mawaior Nov 29 '23 at 19:32