The Talmud (Bava Batra 21a) presents the following dispute:
ואמר רבא האי מקרי ינוקי דגריס ואיכא אחרינא דגריס טפי מיניה לא מסלקינן ליה דלמא אתי לאיתרשולי רב דימי מנהרדעא אמר כ"ש דגריס טפי קנאת סופרים תרבה חכמה
Raba also said: If we have a teacher who gets on with the children and there is another who can get on better, we do not replace the first by the second, for fear that the second when appointed will become indolent. R. Dimi from Nehardea, however, held that he would exert himself still more if appointed: 'the jealousy of scribes increaseth wisdom.' (Soncino translation)
(The translation "gets on with" is not very precise. Indeed, a footnote there says: Lit., 'reads', viz., the prayers or the Scripture.
)
R. Jacob Ben Asher codifies this law in Tur (Y.D. 245) as follows:
ואם יש כאן מלמד שמלמד לתינוקות ובא אחר טוב ממנו מסלקים הראשון מפני השני
And if there is a teacher here who teaches the children and another one comes who is better than him, we remove the first one on account of the second one.
R. Joseph Karo codifies this law in Shulchan Aruch (Y.D. 245:18) as follows:
אם יש כאן מלמד שמלמד לתינוקות ובא אחר טוב ממנו מסלקין הראשון מפני השני
If there is a teacher here who teaches the children and another one comes who is better than him, we remove the first one on account of the second one.
R. Mordechai Yaffe codifies this in Levush (Y.D. 245:18) and elaborates on the Talmudic basis:
אם יש כאן מלמד שמלמד לתינוקות ובא אחר טוב ממנו מסלקין את הראשון מפני השני ולא חיישינן שמא מתוך שזה יראה שדוחין הראשון מלפניו יתפאר בלבו לומר שאין כמותו ויתרשל בתינוקות שלא יירא שמא יסלקוהו דאדרבה כל שכן כשיראה שחשבנוהו יתן לב להיות למד יפה שדואג מקנאת חבירו שסלקוהו מפניו שיביישנו בבני העיר
If there is a teacher here who teaches the students and another one comes who is better than him, we remove the first one on account of the second one. And we are not concerned that perhaps from seeing that the first one was pushed away from before him, [the second one] will glorify in his heart to say that there is no one like him, and he will become lax with the children because he will not be afraid that they will remove him. On the contrary, when he sees that they respect him he will certainly set his heart to teach well because he will worry about the jealousy of his fellow whom they removed from before him, that they will embarrass him among the townspeople.
R. Yechiel Michel Epstein also codifies this in Aruch Hashulchan (Y.D. 245:18) and like R. Yaffe discusses the reasoning:
וכתבו הרמב"ם והטור וש"ע בסעי' י"ח אם יש כאן מלמד שמלמד לתינוקות ובא אחר טוב ממנו מסלקין הראשון מפני השני עכ"ל ונראה דה"ה בהדין הקודם שהרי השני טוב ממנו ויש מי שאינו אומר כן [עי' פ"ת סקי"א] ולא נהירא ודע שדין זה הוא בגמ' שם והכי איתא שם ואמר רבא האי מקרי ינוקי דגריס ואיכא אחרינא דגריס טפי מיניה לא מסלקינן ליה דילמא אתי לאיתרשולי כלומר יתפאר בלבו שאין כמותו ויתרשל וכו' [רש"י ד"ה דילמא] רב דימי אמר כ"ש דגריס טפי קנאת סופרים תרבה חכמה וקיי"ל כרב דימי
And Rambam, and the Tur and the Shulchan Aruch in Se'if 18, write that if there is a teacher here who teaches the children and another one comes who is better than him, we remove the first one on account of the second one, end quote. And it seems that the same applies to the previous law, because [in that case as well] the second one is better than him. And there is someone who does not say this (see Pitchei Teshuvah Seif Katan 11), but he is incorrect. And know that this law is in the Talmud there, and this is what is there: "Raba also said: If we have a teacher who gets on with the children and there is another who can get on better, we do not replace the first by the second, for fear that the second when appointed will become indolent." Meaning that he will glorify in his heart that there is no one like him, and he will become lax, etc. (Rashi s.v. Dilma) "R. Dimi, however, held that he would exert himself still more if appointed: 'the jealousy of scribes increaseth wisdom.'" And we hold like R. Dimi.
It is perhaps noteworthy that Rambam in his code seems to have left out this law (although R. Epstein above does cite Rambam along with the Tur and Shulchan Aruch). R. Joseph Karo notes this in his commentary to the Tur, and says he doesn't know why Rambam left it out:
ולא נתבארו דברים אלו בדברי הרמב"ם ולא ידעתי למה
And these matters are not explained in the words of Rambam, and I don't know why.
In any event, it seems that all the other major codes ruled in accordance with R. Dimi that we do replace a teacher when a better one comes along. In his compendium (Bava Batra 2:9) R. Asher Ben Yechiel explains that we follow R. Dimi in the next dispute mentioned in the Talmud (partly) because of the general rule of following the view of the later Sage. Presumably this is why we follow him in this dispute as well:
והלכתה כרב דימי דבתראה הוא
And the law follows R. Dimi because he is later.