They can be used to mean the same thing at times.
- I'm very thin compared to Fred.
- I'm very thin compared with Fred.
But there are also small differences in meaning at other times.
Often the \"to\" version is used when you want to highlight the
contrast between two things. The \"with\" feels more like you're
simply holding up two things and noting what the differences are
instead of drawing attention to the difference.