Even though the question has been more or less answered by Rachmaninoff in the comments, I would like to provide an alternative approach to the idea:
Let $k$ be an algebraically closed field and let $Z_1,Z_2$ be classical affine $k$-varieties. Then the disjoint union $Z_1\amalg Z_2$ (the coproduct in the category of spaces with $k$-valued functions¹) is also classical affine. Indeed, denote $A_i=\Gamma(Z_i,\mathcal{O}_{Z_i})$ to the coordinate ring and define $A=A_1\times A_2$. Then $A$ is a reduced $k$-algebra of finite type (because so is $A_i$). Hence, $A$ is the coordinate ring of a classical affine variety $Z$. For $i=1,2$, define $e_i\in A$ as $e_1=(1,0)$, $e_2=(0,1)$. Then $Z=D(e_1)\amalg D(e_2)$ (see for instance here). By Proposition 3.32 of Milne's notes, we have that $D(e_i)$ is affine and with coordinate ring $A_{e_i}$. By the isomorphism
$$(S_1\times S_2)^{-1}(R_1\times R_2)\cong S_1^{-1}R_1\times S_2^{-1}R_2$$
(where $R_i$ are rings and $S_i\subset R_i$ is a multiplicative subset; see this), it follows $A_{e_i}\cong A_i$. By the equivalence of categories between affine varieties and reduced $k$-algebras of finite type, $D(e_i)\cong Z_i$.
This constitutes an alternative proof of the isomorphism $K[X \cup Y]=K[X] \times K[Y]$ of your question: if $X,Y\subset\mathbb{A}_k^n$ are algebraic sets that are disjoint as sets, then it is easy to verify that the affine variety their union gives rise to is the disjoint union as spaces with functions of $X$ and $Y$.
(Note that the infinite disjoint union of affine algebraic varieties is never affine for it is not quasi-compact.)
¹ For the terminology, see first chapter of Görtz, Wedhorn, Algebraic Geometry I: Schemes, 2nd edition. In his notes, Milne calls a “(morphism of) $k$-ringed space(s)” to what Görtz, Wedhorn (and me here) call “a (morphism of) space(s) with functions.” You can also read about the sheaf-theoretic approach to classical algebraic geometry in the notes of Gathmann and Ellingsrud, Ottem.