Please explain the concept of shirayim (literally, leftovers) wherein the followers of a chassidic Rebbe will receive portions of his food to it, these leftovers are considered holy or special in some way. Where does the concept of this ritual come from? Why is it only performed on shabbat and holidays? Is it specific to food (if so, why)? What are some of the first documented cases of shirayim being given out?
To quote Rav Aviner here:
It has no clear source in the Gemara or in the Poskim. Some learn it from the Gemara Chagiga (13b) that it is forbidden to eat a loaf of bread from which a mouse nibbled since the impurity has spread throughout, and this is all the more so when a righteous person eats from it that the purity spreads throughout (Ha-Rav Avraham Shapira explained it in this manner [...]
Or the Yerushalami Moed Katan (2:3) which says that after the meal celebrating the New Month, Rebbe Yohanan would collect the crumbs and eat them, saying: "May I spend my life in the next world together with those who ate here last evening" (Brought in Shaarim Metzuyanim Be-Halachah 42:2.)
This Yerushalmi is mentioned by the Munkatcher Rebbe, Rav Chaim Elazar Shapira, in his commentary there as the source for shirayim.
In Sanhedrin 92a, there is some discussion on the value of giving out bread to others and leaving behind bread prior to saying the blessings after meals:
ואמר רבי אלעזר: כל הנותן פיתו למי שאין בו דעה - יסורין באין עליו
R. Eleazar said: Whoever gives of his bread to one who lacks knowledge will be assailed by suffering [...]
ואמר רבי אלעזר: כל שאינו משייר פת על שלחנו - אינו רואה סימן ברכה לעולם
R. Eleazar also said: He who leaves no bread on the table [at the end of his meal] will never see a sign of blessing
Shmuel Barzilai asserts that the custom of shirayim during a Shabbat tisch [chassidic gathering with a rebbe] comes from this gemara. In his work, Chassidic Ecstasy in Music, he records the custom and the spiritual significance of it:
The Sages also said: "Whoever does not partake of bread at his [the Tzaddik's] table has never seen a blessing in his lifetime." The Chassidim receive the Tzaddik's shirayim and what is left of the meal is shared among them, as the Tzaddik's act of eating is altruistic, and the Chassidim would almost give up their souls to receive shirayim from the Tzaddik's hands.
The Kabbalah endows mystical importance to these shirayim, as food contains "elevation of the sparks." The Besht said:
Why did the Holy One, Blessed be He, create man with the ability to become hungry and thirsty when He could have created a man able to live without food or drink? For it is said, 'their souls fainted in them' [Ps. 107:5]? Therefore, G-d made man hungry for food and thirsty for drink, in order for him to uplift the sparks, that is, the souls that have fainted in [becoming] the food, which is the meaning of 'their souls have fainted in them'.
The Chassidim viewed the shirayim as a meritorious 'charm' for filling them with heavenly awe, livelihood and good health. In some courts of Tzaddikim, a second table was set as soon as the first was complete. The second 'round' offered various delicacies in honor of the Shabbat. At this second setting, the Tzaddik would discuss issues with his Chassidim and relate Chassidic tales. The sense was one of "brethren sitting together" [Ps. 133:1]. This second setting was usually lengthy, lasting until well into the night, and was one of the most beautiful periods of the whole Shabbat. (Barzilai 89)
For an alternative, simpler explanation, Steven Bayme argues that the origin of shirayim developed from the belief that a tzaddik could perform wonders, so picking up his crumbs could "provide physical closeness and connection to the rebbe" (Understanding Jewish History 248). Similarly, in his Hakira article "Hasidism and the Rebbe/Tzaddik: The Power and Peril of Charismatic Leadership," Elijah Judah Schochet writes that:
Hasidim believed that such edibles contain magical power as they had been touched and sanctified by the Rebbe/Tzaddik. Shirayim were believed to be a segula for the ill, and the verse “He will bless your bread” [Ex. 23:25] was interpreted to mean that as a result of eating sanctified food “I (G-d) will remove all disease from your midst.” [cf. Kalonymous Kalman HaLevi Epstein, Meor va’Shemesh, “Mishpatim.”]
And then there are other explanations for the custom, summarized by R. Ari Enkin and noted by user2083. I'm not familiar with the first recorded source of the custom, but regarding the reason for its performance on Shabbat and holidays, it is likely due to the fact that rebbes were particularly accessible to their chassidim during this time.
The practice was passed on via an un-named Jewish Town-Proprietor who passed the practice onto the Besht. The Proprietor told the Besht that the meal is to see-out the sabbath Neshamah (a kind of death) among one's people Israel. Although the Proprietor's name is not mentioned, the practice originated with the medieval Ashkenazi Hassidim expression of the Hitveadut for the Seudah Shlishit of the Sabbath and Yom Tov. The Kos Shel Bracha following such meals was combined with Havdalah a practice whose order is the subject of debate between Shammai (Candles, Birkat HaMazon, Besamim, Havdalah) and Hillel (Candles, Besamim, Birkat HaMazon, Havdalah). The Shirayim of such meals was usually breadcrumbs which were put into the Kos shel Brakha with warm water as discussed in the Shulchan Arukh. As long as the tzadik who ate the meal had eaten a KeZayit piece of the bread, they could say Birkat HaMazon while the rest of the bread could be broken up into crumbs up to but not equal to the size of an olive and distributed to the haverim with the Kos Shel Bracha wine and covered by Al HaMichya. Distributing one's shirayim is a kind of tzedakah as a positive mitzvah from Devarim 24:19 and Leviim 23:32. In 17th century Eastern Europe, the Shirayim was so commonly Kugel (particularly Lokshen Kugel) that Kugel became an alternative name for Shirayim in many Hassidic circles and Xians in Poland were prohibited from eating it. Thus we see references to Lokshen Kugel (by which we understand Shirayim) being ordained at Mount Sinai. The transfer of the Tish from Seudah Shlishit to Erev Shabbat was a 19th century innovation.