Why this trick apparently works to make piecewise function differentiable? (also, difference between right derivative and right limit of derivative?) In high school Calculus a few weeks ago we were given the following problem - it has haunted me ever since and I finally decided to ask about it here:
$$f(x) = \left\{\begin{array}{ll}
2x^2+x+1\ &\text{if }x\leq 0,\\
ax+b &\text{if }x>0.
\end{array}\right.$$
1. Determine the values of $a$ and $b$ such that $f$ is continuous for all x-values.
2. Determine the values of $a$ and $b$ such that $f$ is differentiable for all x-values. (Hint/reminder: for $f$ to be differentiable, both $f$ and $f'$ must be continuous.)

Part 1 was easy and I believe I understand why it works. And clearly $b=1$ because that's the solution to $a(0)+b = 2(0)^2+0+1$. 
Part 2 was conceptually much more difficult. The first issue is that the statement in the question that "for $f$ to be differentiable ... $f'$ must be continuous" is demonstrably false. Being the math nerd that I am, I remembered quickly that in fact the function $f(x)=x^2\sin(1/x)$, where $f(0)=0$, is differentiable at $x=0$ but nevertheless $f'$ is NOT continuous there.
The second tricky thing is that even though my teacher's premise was apparently false, the solution they derived from that premise was apparently correct! (or at least I have no reason to believe it was incorrect) Specifically, they set the derivative at $x=0$ of the top expression, $4(0)+1$, equal to that of the bottom expression, $a$, and quickly found $a=1$ ($b$ is, of course, still 1 as well). 
That's a really nifty trick -- just set the derivatives of the two expressions equal to one another and solve. So at this point I'm super confused. Assuming the method my teacher used did in fact work, why did it work? 
Furthermore, when researching this topic I came across the idea of right and left-hand derivatives. My understanding is that those two terms are related to, but distinct from, the left and right-hand limits of the derivative function at a  point. In other words, my understanding is that the left derivative at $x=a$ is defined as....
$$f'_{-}(a)=\lim_{h\to 0^-}\frac{f(a+h)-f(a)}{h}$$
... while the limit of the derivative function as $x$ approaches $a$ from the left is simply... $$\lim_{x\to a^-}f'(x)$$
Am I correct that there's a difference between those two concepts (and, regardless, does it matter to my ultimate inquiry?)
I am aware that others have asked somewhat similar questions here in the past (e.g. here and here). However, the answers I have seen either gloss over things I'm confused about or otherwise have not helped me achieve full understanding. 
I'll note, lastly, that I've also tried to think about this problem graphically. I suppose that has shown me that the teacher's solution was somewhat sensible (it did give me an intuitive idea that the "slopes" need to "match" on each side), but it has not completely convinced me in the way a formal justification would. Furthermore, oftentimes it just takes me back in circles to the difference (if any) between a right-hand derivative and the right-hand limit of the derivative function. 
 A: For continuity, the only point of concern is $x=0$ and we need
$\lim_{x \uparrow 0} f(x) = 1 =  \lim_{x \downarrow 0} f(x) = b$.
Hence $f$ is continuous everywhere iff $b=1$.
Since $f_-(x)= 2x^2+x+1$ and $f_+(x)= ax+b$ are both smooth everywhere, the only way that $f$ can fail to be differentiable at $x=0$ is if $f_-'(0) \neq f_+'(0)$.
Hence we need $f_-'(0) = 1 =  f_+'(0) = a$.
It is not true in general that $f'$ needs to be continuous. In fact, $f'$
may only exist at one point. For example, take $f(x) = x^2 1_\mathbb{Q}(x)$ (indicator function of the rationals multiplied by $x^2$), then $f$ is differentiable at $x=0$ but is not even continuous for $x \neq 0$.
In general, if one has $f_-$ and $f_+$ (defined for $x \le 0$ and $x>0$), then to be differentiable at $x=0$ we need $\lim_{x \uparrow 0} f_-(x) = f_-(0) = \lim_{x \downarrow 0} f_+(x)$ (this is just continuity) and
$\lim_{h \uparrow 0} {f_-(h) - f_-(0) \over h} = \lim_{h \downarrow 0} {f_+(h) - f_-(0) \over h}$.
In the above case, these two limits are just $f_-'(0), f_+'(0)$.
Answer to questions in comments:
In general, suppose we are given $f$ defined as $f_1$ for $x \le 0$ 
and $f_2$ for $x>0$.
Note that the value of $f$ at $x=0$ is $f_1(0)$. (Here, $0$
is not even in the domain of $f_2$.)
Then $f$ is differentiable at $x=0$ iff the following two limits
exist and are equal:
$$ \lim_{h \uparrow 0} {f_1(h)-f_1(0) \over h} \ \ \ \ \ \ \ \ \lim_{h \downarrow 0} {f_2(h)-f_1(0) \over h} $$
Note that the value at $x=0$ is $f_1(0)$ in both cases.
Now suppose further that there are two functions $g_1,g_2$ that are
differentiable at $x=0$ (that is, two usual sided derivatives at $x=0$),
and that $f_1(x)=g_1(x)$ for $x \le 0$ and $f_2(x)=g_2(x)$ for $x>0$.
(Note that since $g_1,g_2$ are differentiable at $x=0$ then they are continuous at $x=0$.)
Then the above limit conditions are equivalent to showing
$g_1(0) = g_2(0)$ and $g_1'(0) = g_2'(0)$.
Note that to conclude this, we only need $g_1,g_2$ to be differentiable at $x=0$. It is not necessary to have anything like $C^1$.
