Your idea is nearly correct! Looking at specific choices of $\beta'$ and $\beta''$ is the right idea. However, $\beta'$ and $\beta''$ must have the same codomain in order to have the equality $\beta'\circ f = \beta''\circ f$ make sense.
The choice of $\beta'$ you had initially (the projection $B\to B/\operatorname{im}(f)$) is good, but the choice of $\beta''$ needs to be modified slightly (in your initial post, it was the map $B\to\{0\}$). Do you see how to choose $\beta''$ so that $\beta''$ is also a map $\beta'' : B\to B/\operatorname{im}(f),$ but will still guarantee that $f\circ\beta''(a) = f\circ\beta'(a)$ for all $a\in A$?
Hint: Think about what the composition $\beta'\circ f$ is!
Try to figure this out for yourself before looking at the spoilers below! The first box describes an appropriate choice of $\beta'',$ and the second outlines why this shows that $f$ must be a surjection.
Let $\beta''$ be the map which sends each $b\in B$ to $0 + \operatorname{im}(f)\in B/\operatorname{im}(f)$ -- i.e., $\beta''$ is the trivial (zero) morphism from $B$ to $B/\operatorname{im}(f).$
Since $f$ is an epimorphism, we must have $\beta' = \beta'',$ so that $\beta'(b) = \beta''(b) = 0 + \operatorname{im}(f)$ for all $b\in B.$ But $\beta'$ is the canonical projection $B\to B/\operatorname{im}(f),$ so that $\beta'(b) = b + \operatorname{im}(f).$ This implies that $b + \operatorname{im}(f) = \operatorname{im}(f)\in B/\operatorname{im}(f)$ for all $b\in B.$ Unwinding the definition of equality in a quotient group, this means $b\in\operatorname{im}(f)$ for all $b\in B,$ which is precisely what was to be shown.