There is an injunction on returning to Egypt. Why was it and is it ignored? For example, how is it that the Rambam an even Rabbi Ovadiah Yosef lived there?
The Yeraim answers that the ban only applies when leaving Eretz Yisroel for Mitzrayim.
The Ritva has another answer he says the ban only applies when most of the Jews live in Israel.
The Rabbeinu Bechaye says the ban on living in Mitzrayim only applied in the times of the Torah when the Egyptians were infamous for their immorality, latter it would not apply (especially seeing that they are Muslim)
The Sho’el U’meishiv says the ban only applies when the whole of Klal Yisroel returns to Mitzrayim.
The Divrei Shaul by Rabbi Yosef Shaul Halevi Nathansohn says it is only banned if we go back to Mitzrayim by the same route of 42 encampments that the Jews followed in the desert.
The Semag says since Sennacherib and the Assyrians sent away the original Egyptians the ban no longer applies.
For a start, consider what the Rambam says in Hilkhot Melakhim 5:8, to the effect that it is permissible to move there for financial reasons, etc, just not with the intention of settling. And that if one were to then decide to settle, he would not be lashed for this, since his moving there was permissible at the beginning. And if one were to change one's mind and decide not to leave, אין בו מעשה (this is not a "deed"; ie: it cannot be considered a transgression, since you have not actually done anything).
This opinion of his also helps us to understand what he says in halakha 9, to the effect that it is forbidden to leave the land of Israel. Before the Rambam settled in Egypt, he lived briefly in Acre, and made pilgrimage to Jerusalem. But as he says, this prohibition is only in effect at such a time as money is plentiful and life in the land of Israel is good. At all other times, it is permissible to leave in order to make a living.
The scriptural source for the aforementioned, which is alluded to in the following texts, is Deuteronomy 17:16 - "Only let him [the king] not multiply horses for himself, nor send the people back to Egypt in order to acquire horses, as the Lord has said to you: do not continue to return by this path any more".
To get to the essence of your question, so far as the Rambam in particular is concerned, note that this has been asked by others already. For example, R' Meir haKohen of Rothenburg (student of the Maharam; "Hagahot Maimoniot", 13th c.):
ויש תימא על קהילות השוכנים שם, וגם רבינו המחבר עצמו הלך לגור שם, וליכא למימר שטעמו מפני שבא סנחריב ובלבל את העולם כדתניא בתוספתא דקידושין (פ"ה ה"ו) שאמר לו ר' עקיבא לבנינין גר המצרי, שהרי בפרק החליל אמרינן ואלכסנדריאה מ"ט איענוש משום דעברי אהאי קרא לא תוסיפו לשוב, וגם בתוספ' דמס' ידים (פ"ב ה"ח) אמרינן למצרים נתן הכתוב קצבה שנאמר (יחזקאל כט יג) מקץ ארבעים שנה אקבץ את מצרים מן העמים אשר נפוצו שם וישבו על אדמתם. ואין לנו טעם להתיר אם לא נפרש כפירוש רא"ם שפירש לא תוסיפו לא אסרה תורה אלא בדרך הזה כלומר מא"י למצרים אבל משאר ארצות מותר. ע"כ מסה"מ
And one can wonder about the communities that do settle there - and even Rabbeinu the compiler himself went to live there - and it is not possible to say that his reason was that Sennacherib had mixed up the nations [and that therefore "Egypt" no longer exists in the same way that neither does Moab or Ammon, etc], as it says in the Tosefta for Kiddushin (5:6): "[Rabbi Yehuda said that Benjamin was an Egyptian convert and was the associate of one of R' Akiva's students. He said, "I am an Egyptian convert and I married an Egyptian convert. I am going to marry my son to the daughter of an Egyptian convert in order that my grandson will be able to enter the congregation, as it says (Deuteronomy 23:9): 'The third generation will enter the congregation of the Lord'".] Rabbi Akiva said to him, "Benjamin, [you have erred in the halakha. When Sennacherib came he confused all of the nations. Ammonites and Moabites are not in their place, Egyptians and Edomites are not in their place, but an Ammonite marries an Egyptian, an Egyptian marries an Ammonite - any of these can marry any of the families of the world, and any of the families of the world can marry any of these]".
[It is not possible to say the foregoing in this instance] because in Perek HaChalil (Sukkah 51b) it says [about the community in] Alexandria [which was wiped out by Alexander the Macedonian], "What is the reason for their being punished? Because they transgressed, as the verse says (Deuteronomy 17:16): 'You shall not continue to return [by this path - ie: to Egypt]'". Also, in the Tosefta for Yadayim (2:8), it says: "Scripture has given a time for Egypt [by which they will be considered a nation again, despite the deeds of Sennacherib], as it says (Ezekiel 29:13): 'At the end of forty years I shall gather up Egypt from all of the nations where they are scattered and shall return them to their land'".
[Given all of the above,] we would have no means of permitting [settlement in Egypt in the time of the Rambam] were it not for the explanation brought by the Re'em [probably R' Eliezer ben Shmuel of Metz, 12th c.), who explained: the Torah only forbade us from continuing via this path - which is to say, from the land of Israel to Egypt - but from all other nations it is permissible. See also [the Rambam's] Sefer haMitzvot (Negatives #227) [where he forbids selling land in the land of Israel in perpetuity].
Also, the following is brought by R' David ben Avi Zimra (Radbaz, 15th-16th c.):
ויש ליתן טעם שלא אסרה תורה אלא לירד לגור שם ולהשתקע, כדאיתא בירושלמי לישיבה אי אתה חוזר אבל אתה חוזר לסחורה ולפרטמטיא ולכיבוש הארץ, וכל היורדים תחלה לא ירדו להשתקע אלא לסחורה ואע"ג דאחר כך נשתקעו אין כאן לאו אלא איסורא בעלמא ומפני טורח הטלטול ומיעוט הריוח במזונות בשאר המקומות לא חששו לאיסור זה וכן משמע מתחלת לשון רבינו שכתב אסור להתישב בה אלא שמסוף הלשון משמע דאיכא איסור לאו שכתב ואין לוקין על לאו זה וכו' משום שאין בו מעשה. ואפשר שהראשונים היו מפרשים כאשר כתבתי. ואם תאמר תקשי לרבינו שהרי נשתקע במצרים. ויש לומר דאנוס היה על פי המלכות שהיה רופא למלך והשרים. וגם אני נתישבתי שם זמן מרובה ללמוד תורה וללמדה וקבעתי שם ישיבה וכי האי גוונא מותר ושוב באתי לירושלים:
And we can give as the reason that the Torah only forbade us to go down [to Egypt] to live there and to take root, as it says in the Yerushalmi: for settlement, you may not return, but you may return for business and for trade and for conquering the land. And all those who went down originally didn't do so to take root but for business, and even though they subsequently took root this was not a transgression but a general prohibition, and because of the effort of moving their possessions and the loss of income [incurred by going to] other places, they didn't worry about this prohibition. And this is understood from the beginning of Rabbeinu's formulation. He wrote, "It is forbidden to settle there", but at the end of his formulation it is understood not to be a transgressive prohibition since he writes that "we do not lash people for this transgression", etc, since "it is not a deed". And it is possible that the early generations interpreted it as I have written.
And if you say that this contradicts Rabbeinu, since he took root in Egypt, one can say that he was forced by the government, since he was the physician to the king and his ministers. And I also once settled there for some time, to learn Torah and to teach it, and I established a yeshiva there - and in this way it is permissible - and then I returned to Jerusalem.
Apparently, as is brought in מקורות וציונים in the Fraenkel edition, R' Shmuel, one of the Rambam's grandsons, testified that the Rambam used to sign his letters with the words העובר בכל יום ג' לאוין ("who transgresses three prohibitions daily"). Assuming this to be true, this shows that he recognised that it was forbidden to dwell in Egypt, and perhaps supports the view of the Radbaz that he was forced to do so. See below, in the comments to this question, where @mevaqesh brings a variety of counter-arguments against the veracity of this claim, which may be historically spurious. In any case, if you read Joel L. Kraemer's excellent Maimonides: The Life and World of One of Civilization's Greatest Minds (New York: Doubleday, 2008), you'll see that he says the following on p.141:
After leaving Hebron, Moses ben Maimon, with his father and brother, returned to Acre, remaining in that vicinity until May 1166, when they departed for Egypt. Egypt was the center of Mediterranean commerce with a large Jewish population. However, a rabbinic prohibition forbade dwelling in the land of Egypt, and we wonder how the Great Sage could flout a precept that he later codified as a law. Maimonides, so far as I know, did not try to justify his decision to settle in Egypt. His statement of the law contains a rationale. If someone came to Egypt to do business, he was not guilty when he entered. When he settled there, he did in fact transgress, but not in a way that deserved punishment.
The Rambam holds that it is prohibited (and put a post-script after his name as "someone who violates 3 commandments daily")