Which elements of $H^2(M,\mathbb{Z}/2)$ are the $w_2(E)$ for a real bundle $E$?

This is an obstruction theory problem; you regard $v\in H^2(M;\mathbb{Z}/2)$ as a homotopy class of maps $v: M\to K(\mathbb{Z}/2,2)$, then ask if $v$ lifts through the universal Stiefel-Whitney class $w_2:BSO\to K(\mathbb{Z}/2,2)$. The first obstruction is $\beta(v^2)\in H^5(M;\mathbb{Z})$, as mentioned by Theo Johnson-Freyd in the comments, coming from the fact that $w_2^2(E)$ is the mod $2$ reduction of $p_1(E)$ for any real vector bundle $E$. The secondary obstruction is more subtle (it will have indeterminacy given by a choice of integral lift of $v^2$, for example) but I would be surprised if there is no information on it in the literature.

I think the state of the art on this question might still be Teichner's paper

Teichner, Peter, 6-dimensional manifolds without totally algebraic homology, Proc. Am. Math. Soc. 123, No. 9, 2909-2914 (1995). ZBL0858.57033.

Teichner shows that if $\operatorname{dim}(M)\le 5$, then $\beta(v^2)$ is the only obstruction to $v$ being $w_2(E)$ for some real vector bundle $E$. On the other hand, he constructs examples of manifolds in all dimensions $n\ge 6$ with classes $v\in H^2(M;\mathbb{Z}/2)$ such that $\beta(v^2)\neq 0$. He also shows that any $v\in H^2(M;\mathbb{Z}/2)$ which is Poincaré dual to a codimension $2$ submanifold $N\subseteq M$ is $w_2(E)$ for some $E$.

An older paper which also seems relevant is

Suzuki, H., On the realization of the Stiefel-Whitney characteristic classes by submanifolds, Tohoku Math. J., II. Ser. 10, 91-115 (1958). ZBL0107.17001.


To elaborate on Mark Grant's answer, and sticking to the oriented case, there is a fibration sequence (all of infinite loop maps) $$ BSpin \rightarrow BSO \xrightarrow{w_2} K(\mathbb Z/2,2) \xrightarrow{d} BBSpin$$ The homotopy groups of $BBSpin$ are known from Bott periodicity: $\pi_5(BBSpin) = \mathbb Z$, $\pi_9(BBSpin) = \mathbb Z$, $\pi_{10}(BBSpin) = \mathbb Z/2$, etc.

So a first obstruction to lifting through $w_2$ will be the composite $$ K(\mathbb Z/2,2) \xrightarrow{d} BBSpin \rightarrow K(\mathbb Z,5)$$ which identifies as $\beta Sq^2$ as noted above. The next obstruction would live in 9 dimensional cohomology, etc., and so these obstructions would vanish on spaces of dimension 8 or less.

Finally, I believe the answer doesn't change if one considers nonoriented bundles, because $BO \xrightarrow{w_2} K(\mathbb Z/2,2)$ factors through $BSO \xrightarrow{w_2} K(\mathbb Z/2,2)$, if I am not mistaken.