Consider the ordinary least squares fit of $Y$ to $Z$:

Because the mean of univariate data minimizes the sum of squared residuals, this fit must rise from the mean of the $Y$ values associated with $Z=0$ (left hand red point) to the mean of the $Y$ values associated with $Z=1$ (right hand red point). Since $Z$ changes by $1-0=1$, its slope $\beta$ is the difference of these means:
$$\beta = (E[Y\mid Z=1] - E[Y\mid Z=0]).$$
This formula holds whether the variables refer to data or to a bivariate distribution.
However, the usual formula for the slope asserts it equals the covariance of $(Z,Y)$ divided by the variance of $Z$:
$$\beta = \frac{\operatorname{cov}(Y,Z) }{ \operatorname{Var}(Z) }.$$
When $Z$ is Bernoulli$(p)$, its variance is $p(1-p)$. Solving for the covariance in terms of the slope and the variance of $Z$ gives the stated formula:
$$\operatorname{Cov}(Y,Z) = \beta\, p(1-p) = (E[Y\mid Z=1] - E[Y\mid Z=0])\, p(1-p).$$