According to Wikipedia
In the formal language of set theory, the axiom schema is: $$\forall w_1,\ldots ,w_n\forall A\exists B\forall x(x\in B\Leftrightarrow [x\in A\wedge \varphi(x,w_1,\ldots ,w_n,A)]).$$
It also emphasises that
... $B$ is not free in $\varphi$.
Questions: How to incorporate the above in the formalisation? And why does $A$ have to be free in $\varphi$?
$A$ does not have to be free in $\varphi$, but it is allowed to be free in $\varphi$. The notation $\varphi(x)$ means that $x$ is a variable that could occur freely in $\varphi$, but does not necessarily have to.
The better question is why $B$ is not allowed to be free in $\varphi$. This is to avoid the following contradiction: \begin{align} \forall A\exists B\forall x(x\in B\leftrightarrow(x\in A\land x\notin B)) \end{align}