These are sometimes interchangeable, but when you are stressing similarities between the items compared, the most common word is “to”: “She compared his home-made wine to toxic waste.” If you are examining both similarities and differences, use “with”: “The teacher compared Steve’s exam with Robert’s to see whether they had cheated.”