This is a very broad and complex topic, with multiple opinions in every direction, and the practical halacha depends on the topic being studied. Entire books have been written on it, so it is not easy to summarize.
You could ask a separate question on each of the classical issues of disagreement (the mud-mouse, the louse's spontaneous generation, the path of the sun at night, the baby who cannot survive if born in the eighth month, etc.) and would find it being treated differently.
Depending on the specific issue, you will find various Rishonim and Acharonim disagreeing on the approach. Broadly they either
- acknowledge the disagreement of science and gemara and resolve to keep halacha as is (most often)
- acknowledge and suggest to change halacha to reflect the latest understanding (often for pikuach nefesh reasons)
- reinterpret the gemara (sometimes in forced ways) to show it doesn't conflict with science or disagree with the science
- claim science is wrong, or will be proven wrong
R Natan Slifkin wrote extensively on the topic (especially in his books Sacred monsters and Rationalism vs. Mysticism; Schisms in Traditional Judaism).
I think the best summary of his views is that one doesn't change the halacha even if discovering a scientific error for both practical and text-based reasons. He writes here
There aren’t so many cases of halacha being based on scientific error,
and those that exist are fairly well known. Most famous is the
Gemara’s permission to kill lice on Shabbat based on the mistaken
belief that they spontaneously generate. In my book Sacred Monsters, I
discussed various approaches that are taken. Some take the position
that the halacha should be changed accordingly. But others (with whom
I strongly agree) take the approach that the halacha remains in place.
The reason is that the halachic authority of the Gemara was canonized;
we follow it even though in rare cases it may be based on mistaken
beliefs. This is similar to the celebrated case of the oven of Achnai,
where the objectively correct view - as attested by none other than
God Himself - was overruled in favor of the majority. And the reason
for this in turn is that stability is a crucial component of halachic
authority. Such stability comes in some cases from following the
majority, and it also comes from canonizing certain authorities and
texts.
(The only exception is for cases where human life is at stake; thus,
we do not follow the Gemara’s position that an eight-month fetus is
less viable than a seven-month fetus, or that one may not clear away
rubble on Shabbos to rescue someone who is not breathing.)
He brings other proofs here. See also here on MY.