To begin with, the $\in \mathbb{Q}$ simply means "is rational", and likewise, $\notin \mathbb{Q}$ means "is not rational" i.e. "is irrational".
Now, consider the following (slightly simpler) function:
$$h(x) = \begin{cases} x, x \in \mathbb{Q} \\
-x, x \notin \mathbb{Q} \end{cases}$$
This function is continuous only at the point $x=0$, because for any nonzero value $y$, there are an infinite number of irrational numbers between any pair of rationals, so the sequence will not converge and the $\lim_{y \to\ x} h(x)$ therefore cannot exist.
However, as $0$ is rational, we also know that $h(0)=0$ from our definition of $h(x)$. Furthermore, as $h(x)$ is always defined as either $x$ or $-x$, we know that $-|x| \le h(x) \le |x|$. As $\lim_{x \to\ 0} |x| = \lim_{x \to\ 0} -|x| = 0$ the squeeze theorem tells us that:
$$\lim_{x \to\ 0} h(x) = 0$$
As this is equal to $h(0)$, this proves that $h$ is continuous at $0$.
The same general approach can be used to tackle $f(x)$ in the first part of your question; the only difference between $f(x)$ and $h(x)$ is that $f(x)$ where $x \notin \mathbb{Q}$ is defined as $1-x$ rather than $-x$. Note that:
$$\lim_{x \to\ \frac{1}{2}^{+}} f(x) = \lim_{x \to\ \frac{1}{2}^{-}} f(x) = \frac{1}{2}$$
Having dealt with $f(x)$, now only $g(x)$ remains. First, we know that $g(0) = 0$ by definition. We can also see that regardless of whether $x$ is rational or irrational, $\lim_{x \to\ 0} g(x)$ converges to $0$, so clearly it is continuous at $0$. The final remaining step is to prove that $g(x)$ is continuous when $x \notin \mathbb{Q}$. For any irrational value of $x$, we know that $g(x)$ is $0$, so in order for $g$ to be continuous we must establish that the following tends to $0$:
$$g(x) = \frac{1}{q}, x= \frac{p}{q} \in \mathbb{Q}\setminus \{0\} \ and \ p,q \in \mathbb{Z} \ coprime, q>0$$
Remember that $q > 0$. If we wish to take the limit as $x$ approaches an irrational value and we do so with a sequence of rationals (with numerator and denominator coprime), recognize that in order to get our approximations closer and closer to the desired (irrational) value, we will need to invoke larger and larger values of $q$. Finally, note that:
$$\lim_{q \to\ \infty} \frac{1}{q} = 0$$
As this is equal to $g(x)$ when $x$ is irrational, we have proven that $g(x)$ is continuous for irrational $x$.