Generally, use "to" when comparing two things that are essentially different, and use "with" when comparing things that are essentially the same.
For example, "It's like comparing apples to oranges." These two things are essentially different from each other, and so we use "to". Or taking Deborski's example from above, "He compared dry cereal with hot cereal". We use "with" because both items are essentially cereal, if not but for a minor qualitative difference (temperature).
However, it's not necessarily the state of the object that you're carrying that matters as much as the qualities/quantities you're trying to compare. For instance before it was "apples to oranges", but it would be more appropriate to say "He's comparing the shape of an apple with the shape of an orange." because the shapes are extremely similar, thus "with" is more suitable.