The standard definition of well-formed formula (short: wff) for first-order language is:
every atomic formula is a wff
if $\varphi, \psi$ are wff, then $(\lnot \varphi), (\varphi \lor \psi), (\varphi \to \psi), (\varphi \land \psi)$ are wff
if $\varphi$ is a wff and $x_i$ a variable, then $(\forall x_i \varphi)$ and $(\exists x_i \varphi)$ are wff.
Usual conventions for omitting parentheses are straightforward.
Note In order to "apply" a quantifier to a formula $\varphi$, it is not mandatory that the quantified variable is free in $\varphi$. (Of course, quantifying a variable that it is not in the scope of the quantifier has no "practical" effects, i.e. it does not affect the "meaning" of the formula.)
See :