Every texture object you create has target associated with it. When you first bound that object to the context, you passed glBindTexture a specific target enum. GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP, etc. The target you use in your first bind call becomes inextricably linked to the texture object; if you try to bind it with a different target, glBindTexture will fail.
GL_TEXTURE_2D and GL_TEXTURE_2D_MULTISAMPLE are different targets. If you bind a newly created texture object with glBindTexture(GL_TEXTURE_2D, obj), you cannot later bind it with GL_TEXTURE_2D_MULTISAMPLE. And vice-versa.
GLSL samplers types, as you may have noticed, have suffixes on them. Well, those suffixes denote the type of texture that the particular sampler type works with. sampler2D is a floating-point sampler that works with GL_TEXTURE_2D textures. It will therefore only find a texture with the corresponding target.
If you set a sampler2D uniform to use texture unit 0, and bind somethign to GL_TEXTURE_2D_MULTISAMPLE, that sampler will not see it. It will only look for a GL_TEXTURE_2D bound to that sampler.
Now, you may have heard of view textures. These are textures which share storage with other textures. But the view can have different dimensions and even texture targets. You can view a single layer from a 2D array texture as a 2D texture. And so forth.
Among the conversions that you cannot do is view a multisample texture as a non-multisample texture. Or vice-versa.
So if what you described worked, it was undefined behavior. And you cannot rely on it.
That being said:
Is it necessary to compile different versions of the shader
No; you just can't use one variable. It's perfectly valid to have two sampler variables of different types and conditionally decide to sample one vs. the other. If the condition is dynamically uniform (for GL 4.x), you won't even lose your implicit derivatives
The only thing you have to do is make them use different texture units. Don't try to play games where you assign them the same texture unit and have it decide which to use based on what you bound. It doesn't work (or if it does, it's UB).