In the definition above $X \sqcup Y / \sim$ has equivalence classes as elements. So pick $[x] \in X \sqcup Y / \sim$, then $[x] = \{ y \in X \sqcup Y \ | \ y \sim x\}$, and $y \sim x$ if and only if $f(x) = y$.
Now my question is $x \in X \sqcup Y$, but $f$ is only even defined for $A \subseteq Y$, so how can $f(x)$ even be defined for $x \in X \sqcup Y$?

Ok, so let me gather all the info from comments.
Lets start from the begining. If $\{X_i\}_{i\in I}$ is a family of sets then the disjoint union is defined as the set of pairs:
$$\bigsqcup_{i\in I} X_i:=\big\{(x,i)\ |\ x\in X_i\big\}\subseteq\bigcup_{i\in I} X_i\times I$$
Now for any $j\in I$ we have the canonical embedding:
$$\phi_{j}:X_j\to\bigsqcup_{i\in I} X_i$$ $$\phi_{j}(x)=(x, j)$$
This function is injective (and "natural") so we identify $X_i$ with its image $X_i^*:=\phi_i(X_i)$. Also note that $X_i^*\cap X_j^*=\emptyset$ if $i\neq j$ and $\bigsqcup_{i\in I}X_i=\bigcup_{i\in I}X_i^*$ so it is a decomposition of the disjoint union.
With that we can define the topology on $\bigsqcup_{i\in I} X_i$ as follows: a subset $U\subseteq \bigsqcup_{i\in I} X_i$ is open if and only if $\phi_i^{-1}(U\cap X_i^*)$ is open in $X_i$ for all $i\in I$. Note that since we identify $X_i$ with $X_i^*$ then we can also identify corresponding open subsets and the definition can be simplified: $U$ is open iff $U\cap X_i$ is open in $X_i$ for all $i\in I$.
Finally if $f:X_i\to Y$ is some function then it induces $f^*:X_i^*\to Y$ given by $f^*(x, i)=f(x)$. It can be easily shown that topologies agree and $f$ is continuous if and only if $f^*$ is. So again we identify $f$ with $f^*$. The same applies if $(\cdot)^*$ is on codomain of $f$ or in both. We can substitute $X_i$ for $X_i^*$ (and vice versa) however we want.
Back to the original problem. What the author actually says is if $f:A\to X$ is a (continuous) function then he defines a relation on $X\sqcup Y$ by saying
$$(a,2)\sim \big(f(a), 1\big)\mbox{ for }a\in A$$
That's the technicality hidden behind $a\sim f(a)$.
Ex 1. Let $Y=[0,1]$ be the unit interval, $X=\{*\}$ be one-point space and $A=\{0,1\}$. Define $f:A\to X$ by obvious $f(0)=*$ and $f(1)=*$. The resulting $X\sqcup_{f} Y$ is homeomorphic to the 1-dimension sphere $S^1$. Actually more generally if $X=\{*\}$ then $X\sqcup_{f} Y\simeq Y/A$.
Ex 2. Let $X=Y=[0,1]^2\subseteq\mathbb{R}^2$ be a 2-dimensional unit square. Let $A=[0,1]\times\{0\}\cup[0,1]\times\{1\}$ be lower and higher boundary. Define $f:A\to X$ by $f(t, 0)=(t,0)$ and $f(t, 1)=(1-t, 1)$. So $f$ is identity on the bottom and a "twist" on the top. The corresponding quotient space $X\sqcup_{f} Y$ is the Möbius strip.
Ex 3. Let $X=Y=\mathbb{T}^2$ be a two dimensional torus. Cut "small" open disk from each $X,Y$ and define $A$ to be the boundary of the disk in $Y$. Define $f:A\to X$ to be a homeomorphism (onto image) from $A$ to the same circle (being a boundary of the disk that has been cut) in $X$. Then $X\sqcup_{f} Y$ is homeomorphic to the genus-two surface. Of course I omit technical difficulties with definining everything correctly. The construction is more generally known as the connected sum. It can be shown that the connected sum doesn't depend (up to homeomorphism) on the choice of balls (as long as they are small enough, whatever that means).