In your example:
$A = \{1\} \cup (4,5]$ and $B = \{2\} \cup(5,7]$.
The set $\{1\}$ is open in $A$, as it equals $(0,2) \cap A$ and $(0,2)$ is open in $X = \mathbb{R}$.
(it is not open in the order topology on the set $A$, but the question is about subspace topologies not order topologies!)
Similarly $\{2\} = (1,3) \cap B$ is open in $B$, and thus $\{(1,2)\} = \{1\} \times \{2\}$ is open in $A \times B$, and this set is also open in $A \times B$ as a subspace of $\mathbb{R} \times \mathbb{R}$: $\{(1,2)\} = ((0,2) \times (1,3)) \cap (A \times B)$ and the set $(0,2) \times (1,3)$ is open in $\mathbb{R} \times \mathbb{R}$.
I gave a very abstract answer to why it's true here, but I'll try a more direct approach here, the structure of which you see in the above example as well:
$A$ and $B$ have the subspace topologies $\mathcal{T}_A$ resp. $\mathcal{T}_B$ inherited from $X$ resp. $Y$.
Suppose $O$ is open in $A \times B$ in the product topology $\mathcal{T}_A \times \mathcal{T}_B$. So $O= \bigcup_{i \in I} (U_i \times V_i)$, where $U_i$ open in $A$, $V_i$ open in $B$, for all $i \in I$.
By the definition of the subspace topology on $A$ resp. $B$, we write $U_i = \hat{U}_i \cap A$, $V_i = \hat{V}_i \cap B$ for open $\hat{U}_i$ in $X$ and open $\hat{V}_i$ in $Y$ for all $i$.
Then $\hat{O} = \bigcup_{i \in I} (\hat{U}_i \times \hat{V}_i)$ is open in $X \times Y$ in the product topology and $$\hat{O} \cap (A \times B) = (A \times B) \cap \bigcup_{i \in I} (\hat{U}_i \times \hat{V}_i) = \bigcup_{i \in I} ((\hat{U}_i \cap A) \times (\hat{V}_i \cap B)) =\bigcup_{i \in I}(U_i \times V_i) = O$$
So $\mathcal{T}_A \times \mathcal{T}_B \subseteq (\mathcal{T}_X \times \mathcal{T}_Y)_{A \times B}$.
On the other hand, if $O$ is open in $(\mathcal{T}_X \times \mathcal{T}_Y)_{A \times B}$, we write $O = \hat{O} \cap (A \times B)$ where $\hat{O}$ open in $\mathcal{T}_X \times \mathcal{T}_Y$, so $\hat{O} = \bigcup_{i \in I} (U_i \times V_i)$, with $U_i$ open in $X$, $V_i$ open in $Y$. Again we note that
$$O = \hat{O} \cap (A \times B) = (\bigcup_{i \in I} (U_i \times V_i)) \cap A \times B = \bigcup_{i \in I} ((U_i \cap A) \times (V_i \cap B))$$
which is a union of open sets of $\mathcal{T}_A \times \mathcal{T}_B$, so in that topology. This proves the other inclusion.