Continuity of restrictions $f_A:A\to Y$ and $f_B:B\to Y$ does not justify the conclusion that $f$ is continuous.
The extra condition needed for that is that $A$ and $B$ have the so-called gluing property.
Let $\tau$ be the topology on $X$ and let $A$ and $B$ both be equipped with the induced subspace topology.
Then there is a topology $\tau'$ on $X=A\cup B$ prescribed by the rule: $$V\in\tau'\iff [V\cap A\text{ is open in }A]\wedge [V\cap B\text{ is open in }B]$$or equivalently by the rule:$$F^{\complement}\in\tau'\iff [F\cap A\text{ is closed in }A]\wedge [F\cap B\text{ is closed in }B]\tag1$$
Then evidently $\tau\subseteq\tau'$.
In the special case that $\tau'$ is not properly finer than $\tau$ (so $\tau'=\tau$) the sets $A$ and $B$ have the gluing property.
It is the special case in which the following diagram - where all arrows denote inclusions - is a pushout square.
$$\begin{array}{ccc}
A\cap B & \longrightarrow & A\\
\downarrow & & \downarrow\\
B & \longrightarrow & A\cup B
\end{array}$$
If $A$ and $B$ are both closed (or are both open) then $(1)$ makes clear that $A$ and $B$ have the gluing property.