Induction on Borel rank.
The base cases are if $B$ is $\Sigma^0_1$ (open) or $\Pi^0_1$ (closed). Well, $f$ is continuous, so by definition the preimage of an open set is open - that is, the $\Sigma^0_1$ case is handled. Similarly, since the preimage of a complement is the complement of the preimage, the $\Pi^0_1$ case is handled.
Now there are two kinds of induction step:
The point is, in either case $B$ is a countable union (or countable intersection) of Borel sets of lower rank, and since unions and intersections commute with taking preimages, the preimage of $B$ is a countable union or countable intersection of (by the induction hypothesis) Borel sets, and hence Borel.