Assuming that the goal is to minimize the total number of dice rolls, and assuming that the number of outcomes you want has a prime factor not found in any of the dice you have, a good general algorithm is to first roll your largest die as many times as you need to to give a range larger than your target range. If the number you roll is less than or equal to the largest multiple of the target range, then just take it modulo the target range, and you’re done. If it’s in the remainder, then you’ll have to roll more, but you probably won’t need to start from scratch: You’ve still got some entropy left over, and you can use that.
This would probably be clearer by example. Let’s say, for instance, that you have the standard set of D&D dice, and you’re trying to get a 33 (which has 11 as a prime factor, unlike any of the standard D&D dice). The largest die you have is a d20, so that’s all you’ll roll. A single roll of the d20 isn’t enough, since 33 > 20. Two rolls is enough to start with, since 400 > 33.
So you’ve got your two rolls of the d20, and you use that to generate a number from 1 to 400. Now, 396 is the largest multiple of 33 in that range, so if you get a number between 1 and 396, you just take that number mod 33, and you’re done. If you get 397, 398, 399, or 400, you have to roll more… But you still have the equivalent of a 1d4 roll lying around, for free.
Use that d4 and another roll of the d20 to give you a number from 1 to 80. If that number is 66 or less, again, take the modulo, and you’re done. If it’s 67 or higher, you now have the equivalent of a d14 leftover, which you combine with another d20 roll to make a d280, and so on.
Now, this method isn’t guaranteed to be optimal. It might sometimes be better to use the d12 instead, or even the d8. And it doesn’t even touch on the cases where the target range has all of its prime factors in common with your dice, in which case you can guarantee a result without ever needing rerolls. But it’s still a pretty good general algorithm.
Note, incidentally, that it’s never optimal to use the d6, d4, or d2, since those are all factors of 12. And it’s very rarely optimal to use the d8, since all it has to recommend it over the d12 is an extra factor of 2.