The literature that has previously shown that boys outperform girls in math tests has failed to explain the underlying causes of the phenomenon. This math gender gap has been documented to vary across countries, and shown to grow as students advance through school. In this paper I suggest that these patterns may be explained by sample selection caused by gender differences in schooling's opportunity costs, which lead lower-achieving males to drop out. I present and test the implications of a labor supply model that examines the opportunity cost of school attendance and, thereby, the observed math gender gap. Using an exogenous policy change, the launch of a conditional cash transfer program in Colombia, I estimate that sample selection explains between 50 percent and 60 percent of the gap. Estimates of non-parametric bounds show that selection in the lower quantiles of the male distribution explains a significant portion of the gap.