It should be clear that you only need to show that $f^{-1}(V)$ is affine, so you might as well take $X = f^{-1}(V)$ and $Y = \operatorname{Spec}B$. Then to show that $X$ is affine, you can use the criterion for affineness (Hartshorne exercise II.2.17b):
A scheme $X$ is affine if and only if there is a finite set of elements $f_1,\dots, f_r\in A = \Gamma(X,\mathcal{O}_X)$ such that the open subsets $X_{f_i}$ are affine and $(f_1,\dots, f_r) = A$.
You can show that you can take your $f_i$ to be the images of the $b_i$ under the map $f^\sharp : B\to\mathcal{O}_X(X)$ (show that $X_{f^\sharp(b_i)} = \operatorname{Spec} A_{a_i}$). You already know that the $a_i$ generate the unit ideal in $\mathcal{O}_X(X)$ because the $\operatorname{Spec}B_{b_i}$ cover $Y = \operatorname{Spec} B$. I'll leave this part of the proof to you.
Now, to show that $\mathcal{O}_X(X)$ is finitely generated as a $B$-module: let $s\in\mathcal{O}_X(X)$. Then for any $i,$ there exists a set $\{x_{i,j}\}_{j = 1}^{n_i}\subseteq\mathcal{O}_X(X)$ which generate $\mathcal{O}_X(X)\left[\frac{1}{f^\sharp(b_i)}\right] = f^{-1}(B_{b_i}) = \mathcal{O}_{X_{f^\sharp(b_i)}}(X_{f^\sharp(b_i)}) = A_{a_i}$ is a finitely generated $B_{b_i}$-module, so
$$
\frac{s}{1} = \sum_{j = 1}^{n_i} f^\sharp(\beta_j)\frac{x_{i,j}}{f^\sharp(b_i)^{r_{i,j}}}\in\mathcal{O}_X(X)\left[\frac{1}{f^\sharp(b_i)}\right]
$$
for some $r_{i,j}\in\Bbb N_0$, $\beta_j\in B$. Multiply through by an appropriate power $f^\sharp(b_i)^{r_i}$ to clear denominators and get
$$
f^\sharp(b_i)^{r_i} s = \sum_{j = 1}^{n_i} f^\sharp(\gamma_j)x_{i,j}
$$
by absorbing the excess $f^\sharp(b_i)$'s in any one term on the right hand side into $f^\sharp(\beta_j)$ to get $\gamma_j = \beta_j\cdot b_i^{?}$ (now an equality in $\mathcal{O}_X(X)$).
Next, you know that $f^\sharp(b_i)^{r_i} = f^\sharp(b_i^{r_i})$ and that $(b_1,\dots, b_n) = B$, which implies that $(b_1^{r_1},\dots, b_n^{r_n}) = B$ (by a standard argument in commutative algebra, you can adapt Ian's proof here if you're not familiar with the proof). Then there exist $c_i\in B$ such that $\sum_{i = 1}^n c_i b_i^{r_i} = 1$, which implies
$$
s = \sum_{i = 1}^n f^\sharp(c_i)f^\sharp(g_i^{r_i})s = \sum_{i = 1}^n\left(f^\sharp(c_i)\sum_{j = 1}^{n_i} f^\sharp(\gamma_j)x_{i,j}\right).
$$
Thus, the (finite) set $\{x_{i,j}\}_{i,j}$ generates $\mathcal{O}_X(X)$ as a $B$-module.