Reducing from a Turing machine that recognizes is regular to the halting problem
Here's the key: work out what $L(M_2)$ is.

Notice that $M_2$ takes a string $x$ as input, so we are trying to identify which strings $x$ will cause $M_2$ to accept.

There are two cases:


Case 1: If $M$ accepts on input $w$.  Then  $L(M_2)=\Sigma^*$.

In other words, in this case, it doesn't matter what $x$ is.  $M_2$ will always accept on input $x$.  If $x$ has the form $0^n 1^n$, then $M_2$ accepts (in step 1).  If $x$ does not have the form $0^n 1^n$, then $M_2$ also accepts (in step 2).  $M_2$ accepts either way; it accepts on every possible input.
Case 2: If $M$ does not accept on input $w$.  Then $L(M_2) = \{0^n 1^n : n \in \mathbb{N}\}$.

Let's look at why this is.  If $x$ has the form $0^n 1^n$, then $M_2$ accepts in step 1.  If $x$ does not have the form $0^n 1^n$, then $M_2$ runs step 2 and doesn't accept (since $M$ doesn't accept on input $w$).


So, $L(M_2)$ is one of two possibilities: it is either $\Sigma^*$ (everything) or $\{0^n 1^n : n \in \mathbb{N}\}$.  Which of those two possibilities it is will depend upon $M$ and on $w$ (and in particular on whether $M$ accepts on $w$).  Remember that implicitly $M_2$ is dependent upon $M$ and $w$: it's got them hard-coded into its code.

Moreover, $\Sigma^*$ is regular, but $\{0^n 1^n : n \in \mathbb{N}\}$ is not regular.  So if we have a way to tell whether the language of a TM is regular or not, we have a way to tell whether $L(M_2)$ is $\Sigma^*$ or $\{0^n 1^n : n \in \mathbb{N}\}$ -- which in turn means we have a way to distinguish Case 1 from Case 2, or in other words, we have a way to tell whether $M$ accepts on input $w$ or not.

That means, if we have a way to tell whether the language of a TM is regular or not, we have a way to solve the halting problem.  (It's not the same TM; it's a related TM.  The way we tell whether $M$ accepts on $w$ is not by analyzing whether the language of $M$ is regular; we construct some new TM that is related to $M$ and $w$ in a crazy way, and then test whether the language of that new TM is regular or not.)



It's important to remember that $w$ is not an input to $M_2$.  Instead, $w$ is just a constant that is hard-coded into the source code of $M_2$.  It would be like hard-coding the value of pi as 3.1415927 in your code, except here the hardcoded constant is going to be whatever $w$ is.  Each different $w$ corresponds to a different $M_2$.

P.S. I'll also repeat a crucial point from Karolis Juodelė: "Strings are not regular, languages are."