The Shulhan Aruch (Orah Haim 146:1) writes that it is forbidden to leave the synagogue once the Sefer Torah has been opened for the reading. Even if the reading has not yet begun, one may not walk out once the Torah is opened. The Gemara speaks very strongly about this prohibition of leaving during the Torah reading, applying to such a person the verse, "Ve’ozbeh Hashem Yichlu" – "Those who abandon G-d shall be destroyed" (Yeshayahu 1:28).
The Shulhan Aruch adds that it is permissible to leave in between Aliyot. However, the Mishna Berura (Rav Yisrael Meir Kagan, 1839-1933) clarifies that this applies only under certain conditions. Namely, if one has a need to leave the synagogue, and this is done occasionally, and not on a permanent basis, then he may leave. Of course, he should endeavor to make it back before the reading resumes, unless he had already heard the entire Torah reading.
There is, unfortunately, a widespread misconception that it is entirely permissible to walk out of the synagogue in between Aliyot. This is incorrect. As noted, this is permitted only on an occasional basis, when the need arises, and one must ensure to return before the reading resumes.