Use Cauchy's theorem combined with Morera's theorem. The integrals along any triangle contained in one of the open semiplanes is zero due to Cauchy. It remains to prove that the integrals along triangles that intersect the line are zero too.
Decomposing triangles into smaller triangles you can reduce to the case in which the triangle has an edge on the line. Now decompose this triangle into many many little triangles. All triangles that don't touch the line are zero. The ones that do touch are very little, so by continuity the integral on them is arbitrarily small. Take limit as the number of triangles growth and you are done.
[In the image imagine the big triangle to have constant size.]
The characterization of the sets that you can remove is more difficult. There is no good geometric characterization. But they can be characterized using the concept of analytic capacity (See the last section).
when and why is it possible to extend real-valued function to complex-valued functions?
is the wrong question, it's too general. We can't work with just any extension, we need certain regularity properties. We want a holomorphic extension. Technically, for $f$ defined by $(1)$ to be holomorphic it is not neccessary that $u$ is holomorphic, $z \mapsto u(z/2,-iz/2)$ can be holomorphic even if $(z,w) \mapsto u(z,w)$ isn't. But we can always work with a holomorphic extension.
The local version of the extension question becomes
Let $U \subset \mathbb{R}^2$ be open, and $u \colon U \to \mathbb{R}$. When does there exist an open $\Omega \subset \mathbb{C}^2$ with $U \subset\Omega \cap \mathbb{R}^2$ and a holomorphic $\tilde{u} \colon \Omega \to \mathbb{C}$ such that $\tilde{u}\rvert_{U} = u$?
The answer is "if and only if $u$ is real-analytic". That's clearly necessary, since the Taylor expansion of $\tilde{u}$ about a point in $U$ gives a power series expansion of $u$ about that point by restricting to real arguments. And it is sufficient because the Taylor series of $u$ about a point $p \in U$ converges on some neighbourhood $W_p$ of $p$ in $\mathbb{C}^2$ and thus provides a holomorphic extension of $u\rvert_{W_p \cap \mathbb{R}^2}$ to $W_p$. If we take appropriate $W_p$, say balls or polydisks with centre $p$, then all these extensions fit together and provide a holomorphic extension $\tilde{u}$ with domain
$$\bigcup_{p \in U} W_p\,.$$
A global version of the extension question is
Let $u \colon \mathbb{R}^2 \to \mathbb{R}$. When does there exist a holomorphic $\tilde{u} \colon \mathbb{C}^2 \to \mathbb{C}$ with $\tilde{u}\rvert_{\mathbb{R}^2} = u$?
The answer to this question is easy too. If such a $\tilde{u}$ exists, its Taylor series (about $0$, say) converges on all of $\mathbb{C}^2$, and thus $u$ has a globally convergent power series expansion. Conversely if $u$ has a power series expansion that converges on all of $\mathbb{R}^2$, this series converges on all of $\mathbb{C}^2$ and thus provides an entire $\tilde{u}$.
Now, if $u \colon \mathbb{R}^2 \to \mathbb{R}$ is real-analytic, it need not be the case that $u$ has a globally convergent series expansion. Take for example $u(x,y) = \frac{1}{1+x^2+y^2}$. Its extension is meromorphic with the nonempty pole set $\{ (z,w) \in \mathbb{C}^2 : z^2 + w^2 = -1\}$.
But, the $u$ we want to extend is not an arbitrary real-analytic function. It is supposed to be the real part of a holomorphic function, and hence it must be harmonic. [Which yields sanity check 1 for such problems: Is the given function harmonic? If it isn't, it cannot be the real (or imaginary) part of a holomorphic function.] And an entire harmonic function has a globally convergent power series representation. (Take the Poisson integral over a sphere of radius $R$ and expand the Poisson kernel into a power series. This shows that the Taylor series about $0$ converges at least on the ball of radius $R$.)
Thus when we're looking for an entire function with a prescribed real part - and the given function is harmonic - the method always works.
The method can fail if we want to find a holomorphic $f \colon \Omega \to \mathbb{C}$ with prescribed real part $u$ when $\Omega \subsetneq \mathbb{C}$, since $(z/2, -iz/2)$ need not belong to the domain of the holomorphic extension $\tilde{u}$ for $z\in \Omega$ in that case.
Ahlfors gives a short discussion of the method in section 1.2 of chapter 2 in his Complex Analysis.
Best Answer
Let $f$ be such a function, let $u(x,y)=\operatorname{Re}f(x+yi)=x^4-6x^2y^2+y^4$ and let $v(x,y)=\operatorname{Im}f(x+yi)$. Then, by the Cauchy-Riemann equations,$$v_x=-u_y=12x^2y-4y^3\text{ and }v_y=u_x=4x^3-12xy^2.$$Integrating, it's not hard to see that, for some $k\in\mathbb R$, $v(x,y)=4x^3y-4xy^3+k$. So\begin{align}f(x+yi)&=u(x,y)+v(x,y)\\&=x^4-6x^2y^2+y^4+(4x^3y-4xy^3+k)i\\&=(x+yi)^4+ki.\end{align}