How to understand the variance formula? 
How is the variance of Bernoulli distribution derived from the variance definition?

In a Bernoulli distribution, $E(X) = p$. Remember, the Bernoulli takes value $1$ with probability $p$ and $0$ with probability $1-p$, so the expectation of $X$ is $1\cdot p + 0\cdot (1-p) = p$. The expectation of any function of the Bernoulli is similarly $(E(g(x)) = g(1)\cdot p + g(0)\cdot(1-p)$ Thus
$$
\begin{align}
Var(X) &= E\left((X - E(X))^2\right)\\
&= \overbrace{\underbrace{(1-p)^2}_{g(1)}\cdot p}^\textrm{Bernoulli = 1; probability p} + \underbrace{\overbrace{(0-p)^2}^{g(0)}\cdot(1-p)}_{\textrm{Bernoulli = 0; probability (1-p)}}\\
\end{align}
$$
The rest follows what you brought in the question
