Quoting from a paper by Roth and Vakil ($\mathrm{cd}$ stands for cohomological dimension):
Let $S$ be $\mathbb{P}^2$ blown up at a point, and let $X$ be the affine cone over some projective embedding of $S$. Let $Z\subset X$ be the affine cone over the exceptional divisor of the blowup. Then $Z$ is of codimension one in $X$, but $\mathrm{cd}(X\backslash Z) = 1$, so in particular it is not affine. This shows that, conversely, the complement of a Weil divisor in an affine scheme need not be affine...
Note that the complement of an effective Cartier divisor in an irreducible affine scheme is affine so if in addition to the assumptions in the question the ambient scheme is factorial (e.g. regular), then the complement of any pure codimension 1 closed subset is affine.