The greatest common divisor of two numbers is a divisor of each of those, so $\gcd(n,a)$ is a divisor of $n$ for any number $a$.
The point of what you are doing is the following: you are trying to factor $n$, or at least find a proper nontrivial divisor (that is a divisor $d$ such that $1\neq d\neq n$.
If $x^2\equiv y^2\pmod{n}$, then since $n$ divides $x^2-y^2=(x-y)(x+y)$. If in addition $x\not\equiv y\pmod{n}$ and $x\not\equiv -y\pmod{n}$, then $n$ does not divide $x-y$ nor $x+y$. That means that $\gcd(n,x-y)\neq n$ and $\gcd(n,x+y)\neq n$. If $p$ is a prime factor of $n$, then $p\mid (x-y)(x+y)$, and since $p$ is prime, if it divides a product then it divides one for the factors, so you will have either $p\mid x-y$ or $p\mid x+y$. And that means that $p\mid \gcd(n,x-y)$ or $p\mid \gcd(n,x+y)$. And that will mean that $\gcd(n,x-y)$ (or $\gcd(n,x+y)$) will not equal $1$. So you will "detect" a nontrivial factor of $n$ with either $\gcd(n,x-y)$ or with $\gcd(n,x+y)$, provided that $x^2\equiv y^2\pmod{n}$ and $x\not\equiv \pm y\pmod{n}$. This is the heart of Fermat's factorization method.