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

IF $X \sim$ Bernoulli($p$), then what's the distribution of $X^2$? Find it, then find its expectation, that'll be $\mathbf{E}X^2$, and you already have $(\mathbf{E}X)^2$. 
EDIT: the way it's done in the example you gave, it seems that they defined a different rv: $W = (X- \mathbf{E}X)^2$, which takes values $(0-p)^2$ and $(1-p)^2$ with corresponding probabilities.  
