Herman Bavinck gives a superb answer:

“The answer is implied in the providence of God as it pertains to sin. Scripture repeated states that God uses sin as punishment of the wicked (Deut. 2:30; Joshua 11:20; Judges 9:23-24; John 12:40; Rom. 1:21-28; 2 Thess. 2:11-12), as a means of saving his people (Gen. 45:5; Gen. 50:20), to test and chastise believers (Job 1:11-12; 2 Sam 24:1; 1 Cor. 10:13; 1 Cor. 11:19; 2 Cor. 12:7), and to glorify his name (Exodus 7:3; Prov. 16:4; Rom. 9:17; Rom. 11:33). Precisely because God is the absolutely Holy and Almighty One, he can use sin as a means in his hand. Creatures cannot do that; with the least contact, they themselves become polluted no impure. But God is so infinitely far removed from wickedness that he can make sin, as an unresisting instrument, subservient to his glorification. There are countless examples that prove that also in this connection “when two parties do the same thing, it is not the same.” It was God’s will that Shimei cursed David, that Satan tested Job, that Jews and Gentiles wanted to give up God’s holy servant Jesus to death - still in all these iniquities, human creatures are guilty and God is innocent. For even when he wants there to be evil, he only wants it in a way that is holy: though using it, he never commits it. And for that reason, he has also allowed sin into his creation. He would not have tolerated it had he not been able to govern it in an absolute holy and sovereign manner. He would not have put up with it if he were not God, the Holy and Omnipotent One. But being God, he did not fear its existence and its power. He willed it so that in it and against it he might bring to light his divine attributes. If he had not allowed it to exist, there would always have been rationale for the idea that he was it in all his attributes superior to a power whose possibility was inherent in creation itself, for all rational creatures as creatures, as finite, limited, changeable beings, have the possibility of apostatizing. But God, because he is God, never feared the way of freedom, the reality of sin, the eruption of wickedness, or the power of Satan. So, both in its origin and its development, God always exercises his rule over sin. He does not force it, nor does he block it with violence but rather allows it to reach its full dynamic potential. He remains king yet still gives it free rein in his kingdom. He allows it to have everything - his world, his creature, even his Anointed - for evils cannot exist without goods. He allows it to us all that is his; he gives it opportunity to show what it can do in order, in the end, as King of kings, to leave the theater of battle. For sin is of such a nature that it destroys itself by the very freedom granted it; it dies of its own diseases; it dooms itself to death. At the apex of its power, it is, by the cross alone, publicly shown up in its powerlessness (Col. 2:15).” (Bavinck, Reformed Dogmatics Vol. 3, 64-65)