We count Sefiras Ha’Omer at the beginning of the night, at the conclusion of Ma’ariv, but there are different customs regarding the proper time to count – before or after Aleinu. The sefer Sha’arei Rachamim, authored by 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. 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 shul?
The Sha’arei Teshuvah (Orach Chaim 233:1) cites the Arizal, saying 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 davening.
However, the Bach explains (Orach Chaim 133) that Aleinu was added at the conclusion of davening so that it should be the last thing people say before leaving shul as it instills faith in the Oneness of Hashem’s kingship, strengthens our emunah that He will one day remove all the detestable idolatry on earth, and prevents 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 to be the last tefillah said before leaving shul.
In accordance with the Bach, the Chasam Sofer was accustomed not to say Aleinu after Mincha on erev Shabbos since we remain in shul for Ma’ariv. This supports the opinion of those who say Aleinu was instituted as the last tefillah prior to leaving shul. It is for this reason that we do not say Aleinu after Mussaf or Mincha on Yom Kippur since we do not leave shul after those tefillos.
Perhaps this also explains why many have the custom not to recite Aleinu after Shacharis on Shabbos (since we do not leave shul until after the conclusion of the next tefillah – Mussaf).
Yet, some have the custom to say Aleinu after Shacharis on Shabbos and to repeat it after Mussaf. There are also certain sects of chassidus whose minhag is to recite Aleinu after Shacharis on Rosh Chodesh. These customs are in agreement with the opinion of the Arizal that Aleinu was instituted as a part of each tefillah. Since Shacharis and Mussaf are two separate tefillos, Aleinu is recited twice.
Alternatively, 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 Mussaf together and many consider them one tefillah. That would explain why Aleinu is not said after Shacharis.
Based on this, the sefer Harirai Kedem, vol. II, 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 apply the rule of tadir veshe’aino tadir, tadir kodem (the more frequent precedes the less frequent). As Aleinu is part of Ma’ariv, and Ma’ariv is more frequent, Ma’ariv, in its entirety (including Aleinu), should precede Sefiras Ha’Omer. But if Aleinu was instituted as the last tefillah to be said prior to leaving shul, one should first say Sefiras Ha’Omer and then Aleinu.
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. Nusach Ashkenaz recites Aleinu following Kaddish Shalem, after Ashrei and Uva L’tzion, which concludes the tefillah. They say the Shir Shel Yom following Aleinu. Nusach Sephard recites the Shir Shel Yom first, and Aleinu last. In this way, Nusach Ashkenaz seems to be in agreement with the Arizal that Aleinu concludes the tefillah while those who follow Nusach Sephard seem to be in agreement with the Bach, who says that Aleinu is to be said as the last tefillah prior to leaving shul. Interestingly, it is usually Nusach Sephard and not Nusach Ashkenaz who follows the customs of the Arizal, yet here, we find the opposite.