The short answer is that in general you can make the solution set remain the same as you manipulate an equation, but in order to do so you often need to write more than just a single equation at the end of your manipulation.
To give a general answer to the question seemingly requires either a lot of abstract notation or a much greater amount of text. So let's start with specific examples instead.
I'll assume that you are working strictly with real numbers only.
The easy case is any operation that acts as a well-defined injective function defined on all real numbers. For example, we can add $1$ or divide by $2$ on both sides and not gain or lose any solutions in the solution set.
Now let's look at squaring. Consider the equation
$$ x = -\sqrt{y+1} \tag{1a} $$
and what happens if we want to square both sides.
Squaring has all real numbers as its domain, but it is not an injective function.
In general, if you have $A = B$ then $A^2 = B^2,$ but if you know $A^2 = B^2$ then in order to conclude that $A = B$ you must also know that $A$ and $B$ have the same sign.
Equation $(1\mathrm a)$ is therefore equivalent to
$$
x^2 = y + 1 \ \mathbf{and}\ \Big[\text{$x$ and $-\sqrt{y+1}$ have the same sign}\Big]. \tag{1b}
$$
We can simplify this a little by observing that for $-\sqrt{y+1}$ to have a sign,
$y + 1$ must be non-negative, and then the sign of $-\sqrt{y+1}$ is negative or zero.
We therefore have the equivalent statement
$$
x^2 = y + 1 \ \mathbf{and}\ x \leq 0\ \mathbf{and}\ y + 1 \geq 0. \tag{1c}
$$
But notice that $x^2 = y + 1$ already implies that $y + 1 \geq 0,$ so we can simplify further:
$$
x^2 = y + 1 \ \mathbf{and}\ x \leq 0. \tag{1d}
$$
But if we omit the statement $x \leq 0,$ we get a solution set with two values of $x$ for many values of $y$ where there should have been just one $x.$
The reason people say squaring is "unsafe" or that it causes you to get "extraneous" solutions is that the "squared" equation alone is often not sufficient to make a truly equivalent statement. You may need additional conditions.
But suppose instead that we started with the equation
$$ \sqrt5 = \sqrt{1 + x^2}. \tag{2a} $$
We apply the same steps as before to get an equivalent statement,
$$
5 = 1 + x^2 \ \mathbf{and}\
\Big[\text{$\sqrt5$ and $\sqrt{1 + x^2}$ have the same sign}\Big]. \tag{2b}
$$
Now we note that in fact $\sqrt5$ and $\sqrt{1 + x^2}$ are both positive for all real $x,$ so the part after the "and" is always true and we can omit it without changing the truth of our statement in any case. The simplified statement is
$$
5 = 1 + x^2, \tag{2c}
$$
completely equivalent to Equation $(2\mathrm a)$ and having the same solution set.
Now let's look at square roots.
Consider the equation
$$ x = y - 1 \tag{3a} $$
and let's see what happens when we take a square root on both sides
(ignoring the question of whether this is a useful thing to do).
The difficulty is that the domain of the square root function is non-negative numbers only, and there is no indication in the given equation whether either side is positive or negative. We therefore have to consider both cases.
You could, for example, produce the following statement, which is equivalent to Equation $(3\mathrm a).$
$$
\Big[ x \geq 0\ \mathbf{and}\ y - 1 \geq 0\ \mathbf{and}\ \sqrt x = \sqrt{y - 1} \Big]
\ \mathbf{or}\
\Big[ x < 0\ \mathbf{and}\ y - 1 < 0\ \mathbf{and}\ x = y - 1 \Big]. \tag{3b}
$$
This covers the case where you can take the square root and the case where you cannot, including the conditions required for each case.
You could simplify this a bit by noting that if the equation
$\sqrt x = \sqrt{y - 1}$ is true, both $x$ and $y - 1$ must be non-negative, since otherwise one or both of the square roots would be undefined and could not be equal to anything. The conditions $x \geq 0$ and $y - 1 \geq 0$ are therefore redundant.
You could also observe that $x < 0$ and $x = y - 1$ together imply $y - 1 < 0,$
so $y - 1 < 0$ is redundant if the other two statements are included.
The statement then simplifies to
$$
\sqrt x = \sqrt{y - 1}\ \mathbf{or}\ \Big[ x < 0\ \mathbf{and}\ x = y - 1 \Big]. \tag{3c}
$$
If you try to simplify it further by dropping the part after the "or," you'll lose the part of the solution set where $x$ is negative, which contains an infinite number of solutions of Equation $(3\mathrm a).$
On the other hand, if we start with Equation $(2\mathrm c)$ from the "squaring" discussion and apply this method to it, we get
$$
\Big[ 5 \geq 0\ \mathbf{and}\ 1+x^2 \geq 0\ \mathbf{and}\ \sqrt5 = \sqrt{1+x^2} \Big]
\ \mathbf{or}\
\Big[ 5 < 0\ \mathbf{and}\ 1+x^2 < 0\ \mathbf{and}\ 5 = 1+x^2 \Big].
$$
But we know that $5 \geq 0$ and $1+x^2 \geq 0$ for all real numbers $x.$ Hence the conditions $5 \geq 0$ and $1+x^2 \geq 0$ are redundant while the conditions $5 < 0$ and $1+x^2 < 0$ are contradictions and the entire clause after the "or" can be eliminated. So the complex statement above simplifies to just Equation $(2\mathrm a).$
Again we find that the two equations are equivalent and have the same solution set, although we worked from the opposite direction this time.
Revisiting what happens when we square both sides of an equation, the extra information about the signs of the two original quantities arises because squaring is not injective on the set of all real numbers. But squaring is injective on the domain of non-negative real numbers, and it is injective on the domain of non-positive real numbers.
In a sense, the part of the statement after the first "and" in $(1\mathrm b),$ $(1\mathrm c),$ $(1\mathrm d),$ or $(1\mathrm c)$ turns squaring into an injective function by putting a restriction on the domain from which its input values can come.
Suppose you want to take the cosine of both sides of any equation without getting extraneous solutions. To do that, you have to add a condition such as the statement that there is some integer $k$ such that both sides of the original equation are between
$k\pi$ and $(k + 1)\pi.$
When its domain is restricted to one such interval, the cosine function is injective and therefore "safe."
The general case is that you want to solve
$$ A(x,y,\ldots) = B(x,y,\ldots) \tag{4a} $$
where $A(x,y,\ldots)$ and $B(x,y,\ldots)$ are mathematical expressions possibly involving $x,$ $y,$ and/or other variables.
There is then a step in many derivations where we "do the same thing to both sides," that is, we apply some function $f$ (which could be "add $1$," "divide by $x$," "square it," or any of many other possibilities). We can then conclude that
$$ f(A(x,y,\ldots)) = f(B(x,y,\ldots)) \tag{4b} $$
provided that
- The function $f$ actually exists. ("Divide by $x$" is not a function in the case where $x=0$.)
- $A(x,y,\ldots)$ and $B(x,y,\ldots)$ both are in the domain of $f.$
If it is possible that either $A(x,y,\ldots)$ or $B(x,y,\ldots)$ is not in the domain of $f,$ we need to divide the problem into cases much as we did in $(3\mathrm b)$ so that we can account for the case in which $f$ cannot be applied.
But this gives us an implication in only one direction:
$(4\mathrm a) \implies (4\mathrm b).$
To truly have the same solution set, we need the two expressions to be completely equivalent, that is, we also need
$(4\mathrm b) \implies (4\mathrm a).$
We can do that if and only if we can map the values on each side of
$(4\mathrm b)$ back to their original values in $(4\mathrm a).$
That is, we need the existence of an inverse function $f^{-1}$ such that
$$f^{-1}(f(A(x,y,\ldots))) = A(x,y,\ldots)$$
and
$$f^{-1}(f(B(x,y,\ldots))) = B(x,y,\ldots).$$
Such a function exists only if $f$ is injective. (We usually say $f$ has to be injective and surjective, that is, one-to-one and onto, but we can make any function "onto" simply by restricting its codomain to be the same as its range. In any case, we know that
$f(A(x,y,\ldots))$ and $f(B(x,y,\ldots))$ are both in the range of $f$ because we got them by applying $f$ to both sides of the original equation.)
When the original $f$ is not injective, we can subdivide its domain into pieces on which it is injective. For the function $f(t) = t^2$ two such pieces are the non-negative numbers and the non-positive numbers.
Provided that $A(x,y,\ldots)$ and $B(x,y,\ldots)$ are always in the domain of $f,$
from $(4\mathrm a)$ we can derive an equivalent statement
$$
f(A(x,y,\ldots)) = f(B(x,y,\ldots)) \ \mathbf{and}
\ \left[\begin{array}{l}\text{$f$ restricted to a domain}\\
\text{containing both $A(x,y,\ldots)$} \\
\text{and $B(x,y,\ldots)$ is injective}\end{array}\right].
\tag{4c}
$$
The tricky part, which will require you to look at each non-injective function individually, is to figure out the part of $(4\mathrm c)$ in the square brackets.
If we cannot guarantee that both $A(x,y,\ldots)$ and $B(x,y,\ldots)$ are in the domain of $f,$ we need the more general statement
$$
\begin{align}
&\left[f(A(x,y,\ldots)) = f(B(x,y,\ldots)) \ \mathbf{and}
\ \left[\begin{array}{l}\text{$f$ restricted to a domain}\\
\text{containing both $A(x,y,\ldots)$} \\
\text{and $B(x,y,\ldots)$ is injective}\end{array}\right]\right] \\
&\ \mathbf{or}
\Bigg[ A(x,y,\ldots)) = B(x,y,\ldots) \notin \operatorname{dom}(f) \Bigg] .
\end{align}
$$
And now you know why we don't like to give a completely general answer to this question.
It's often easier to just accept $(4\mathrm a) \implies (4\mathrm b)$ and check the solution set of $(4\mathrm b)$ to eliminate the "extraneous" values.