Q:

Reducing from a Turing machine that recognizes is regular to the halting problem

I'm trying to understand reduction, this is from my textbook and is not a homework problem or even any exercise, just trying to understand an example they present.
This is the reduction they give: 
PROOF We let R be a TM that decides REGULARTM and construct TM S to
decide ATM . Then S works in the following manner.
S = “On input $M$, $w$ , where M is a TM and w is a string:

Construct the following TM $M_{2}$ .
$M_{2}$ = “On input x:

If x has the form $0^{n} 1^{n}$ , accept .
If x does not have this form, run M on input w and
accept if M accepts w.”

Run R on input $M_{2}$ .
If R accepts, accept ; if R rejects, reject .”

So, we start with a machine that decides whether a language of a TM is regular. And we want to use that to decide if a TM halts on a given input.
My question: What if $w$ does have the form $0^{n} 1^{n}$? Well, $M_{2}$ accepts that string just cause of the form. But we never actually run $M$ on $w$. So how can we say that it will accept or reject it? We have no idea what it does because we never ran it on $w$.

A:

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."

