The command in question, given by Moses, is in Numbers 31:17-18 "Now therefore kill every male among the little ones, and kill every woman that hath known man by lying with him. But all the women children, that have not known a man by lying with him, keep alive for yourselves."
To understand this command, a little background is necessary. The situation is that the Medianites are trying to destroy the Israelites, and Moses orders the Israelites to kill the Medianites in order to remove the threat, but to spare the lives of the females. But the adult female captives start trying to lure the Israelites into practicing idolatry. So Moses changes his command, and orders them to kill adult females as well, but to spare the female virgins. Now, in Medianite culture, all females were married off as soon as they reached puberty. So the virgins being spared here are pre-pubescent children, even infants in many cases. For the critics to suppose that "keep alive for yourselves" means to have sex with them is an awfully demented assumption. What God is ordering the Israelites to do is adopt them, which is the most humane thing they could do. Their only other choices would be to kill them or to let them fend for themselves with no parents. Despite how the verse is perceived by critics of the Bible, this is a command to show mercy, not a command to do harm.