Like they match the general gist of each other but not very close.
They’re done by different translators. Typically a dub translator will try to match mouth movements, which obviously doesn’t matter for subtitles. This leads to the subtitles being (typically) a more direct translation.
Generally, when this happens it’s because the subtitles are more “accurate” to the original language and the dub tends to take some liberties with the translation to make it make more sense in the target culture.
One example I can think of is in the anime Cowboy Bebop. In the dub a character calls something pudding and in the original Japanese (and thus the subtitles) they call it red bean paste.
Red bean paste just…isn’t really a thing in the states that most people would know about (especially almost 20 years ago). But pudding is.
Additionally, dubbing tends to try to match the mouth movements (at least for animated stuff for dubbing live action that’s less of an expectation). Whereas the subtitles don’t need to do that. That’s where a lot of the subtle variations can happen.
My (very limited) understanding is that the english dub is spoken to try and match the mouth movements of the character speaking, whereas the sub is more of a translation of the language being spoken.
Other answers already have most of it, but one additional consideration is speaking time. Not just matching mouth movements, but the time it takes for someone to deliver their line.
Some languages tend to be more information-dense than others, and some dialects are spoken at a pretty fast clip. If a line delivered in Chinese would take twice as long to say in English, they’re going to have to take some liberties to get the same information across in the limited time before the next character starts speaking, or the next scene transition, or… etc…
But since people read faster than they speak, you don’t have to bother with rephrasing subtitles if you don’t want to. You can just put up an exact translation.
Additionally, subtitles are done for the original distribution of the film, while a dub is a larger production to make a localized version. The original subtitles are often written before the dub exists, and the dub is recorded with additional considerations that often require changes to the script. They *could* go back and re-subtitle the original to match the dubbed version, but if someone’s choosing to watch the subtitled version over the dub, they probably want to read the closest translation, not a hacked up version.
This is a pretty common thing in many languages/translations. You usually have different companies doing the dubbing than the ones doing subtitles. Additionally, dubbing might be more inclined to change dialog so mouth movements match.