As Felipe and Karl correctly pointed out, the formula you write is not true in general. However, if you read Vakil's paper, you see that he's considering a very particular situation, namely Galois coverings with Galois group $G=(\mathbb{Z}_p)^3$, where $p=2,3$.
Let me explain how the formula works in a simpler case, namely $G=(\mathbb{Z}_2)^2$, the so-called bidouble covers (see Catanese' paper [Ca1] in Vakil bibliography). Then you can try to prove it in Vakil's cases (maybe after reading Pardini's paper on abelian covers).
Let us call $\chi_i$, $i=1,2,3$ the three non-trivial characters of $G$. Then the branch locus $D$ of $f$ can be written as
$D=D_1 + D_2 +D_3$,
where $D_i$ correspond to $\chi_i$. The divisors $D_i$ are smooth and intersect transversally.
Now we can factor $f$ as
$X \stackrel{g}{\longrightarrow} Z \stackrel{h}{\longrightarrow} Y$,
where $h$ and $g$ are double covers branched over $D_1+D_2$ and $h^*D_3$, respectively. Note that in general the intermediate cover $Z$ is singular!
By using the formulae for double covers, we can write
$2K_Z=h^*(2K_Y+D_1+D_2), \quad 2K_X=g^*(2K_Z+h^*D_3)$
that is, putting things together,
$2K_X=g^*h^*(2K_Y+D_1+D_2+D_3)=f^*(2K_Y+D)$.