It is customary to perform the mitzvah of counting Sefiras Ha’Omer at night’s onset, at the conclusion of Ma’ariv. There are different customs regarding the proper time to recite the counting – before or after Aleinu. The sefer Sha’arei Rachamim, from the Vilna Gaon, says that one should first say Aleinu and then count Sefiras Ha’Omer. The Chok Yaakov (489:20), the Pri Migadim (Aishel Avraham 489:15), and the Mishnah Berurah (489:2) say that the proper order is to count Sefiras Ha’Omer first, followed by Aleinu.
In order to better understand the basis for this machlokes, we must look at the origin of the custom to say Aleinu at the end of davening. The question is, was Aleinu instituted to be the conclusion of each tefillah, or was it instituted to be the last prayer that we recite prior to leaving the shul?
The Bach, in Orach Chaim 133, explains that Aleinu was added at the conclusion of davening before people left the shul, in order to instill faith of the oneness of Hashem’s kingship and in order to strengthen our emunah that He will one day remove all of the detestable idolatry from the earth. This will prevent Jews from being tempted to follow the beliefs and lifestyles of the nations among whom they dwell and with whom they engage in business. It seems clear that the Bach’s opinion is that Aleinu was instituted primarily as the last tefillah prior to leaving the shul.
The Sha’arei Tshuvah, in Orach Chaim 233:1, cites the Arizal, that one must recite Aleinu after each of the three tefillos of the day. The Arizal seems to hold that Aleinu was instituted as part of the davening.
The Chasam Sofer had a minhag not to say Aleinu after Minchah on erev Shabbos, since we remain in shul for Ma’ariv. This is in accordance with the Bach who said Aleinu was instituted as the last tefillah prior to leaving the shul. It is for that reason that we do not say Aleinu after Musaf or Minchah on Yom Kippur, since we do not leave the shul after these tefillos.
Perhaps this is the reason why many have the custom to not recite Aleinu after Shacharis on Shabbos, even though it is the end of a tefillah, since we do not leave the shul until after the conclusion of the next tefillah – Musaf. There are those that have the custom to say Aleinu after Shacharis on Shabbos and to repeat it after Musaf. This custom is in accordance with the opinion of the Arizal, that Aleinu was instituted as a part of each tefillah.
It is possible that even the custom of not saying Aleinu after Shacharis on Shabbos is in accordance with the Arizal, since we daven Shacharis and Musaf together, and thus they are considered as one tefillah. Therefore, one need not say Aleinu after Shacharis, since it is not entirely an independent tefillah.
Based on this, the sefer Harirai Kedem explains that we can understand the machlokes regarding when to count Sefiras Ha’Omer in respect to Aleinu. If Aleinu is a part of the tefillah, it should precede Sefiras Ha’Omer, since Sefiras Ha’Omer is a mitzvah that can be performed after Ma’ariv. We can therefore apply the rule of tadir veshe’aino tadir, tadir kodem (the more frequent precedes the less frequent). As Aleinu is a part of Ma’ariv and Ma’ariv is more frequent, Ma’ariv in its entirety (including Aleinu) should precede Sefiras Ha’Omer. Whereas if Aleinu was instituted as the last tefillah to be said prior to leaving the shul, one should first say Sefiras Ha’Omer and allow for Aleinu to be the last tefillah said prior to leaving the shul.
I would like to suggest that perhaps this is the basis for the different customs regarding the order in which we recite Aleinu and the Shir Shel Yom at the conclusion of Shacharis each day. Most people who daven nusach Ashkenaz recite Aleinu following Kaddish Shalem after Ashrei / Uva L’tzion, which concludes the tefillah. They say the Shir Shel Yom following Aleinu. Sephardim and those who daven nusach Sephard recite the Shir Shel Yom first and Aleinu last.
Nusach ashkenaz seems to be in accordance with the Arizal that Aleinu concludes the tefillah. The Sephardim and nusach Sephard seem to go in accordance with the Bach that Aleinu is to be said as the last tefillah prior to going out among the nations among whom we dwell. This is interesting, as it is usually nusach Sephard and not nusach Ashkenaz that follows the customs of the Arizal. Here we see the opposite.