In the following diagram let the map $A \hookrightarrow X$ and the maps to $X \cup Y$ be the inclusions. The maps $\bar f$ and $j: Y \hookrightarrow X \cup_f Y$ send a point to its equivalence class, so the outer rectangle and the triangles commute. Note that closedness of $\pi$ is equivalent to closedness of both $\bar f$ and $j$, so showing that each of these two maps is closed is maybe less messy.

Consider closed sets $B \subseteq X$ and $C\subseteq Y$. You need to show that $\pi^{-1}(\bar f(B))$ and $\pi^{-1}(j(C))$ are closed in $X \cup Y$. For either set, there exists a simple formula from which you should see, using closedness of $A$ and of $f$ whenever needed, that the set is closed. Can you find them? Let me know if you struggle and I'll provide some hints.