In describing the consecration of the Leviyim and their initiation into their role as assistants to the Kohanim in the Mishkan, the Torah indicates that their service too would be part of the process needed to effect atonement for Bnai Yisrael (במדבר ח:י"ט). The Gemara in Erchin (דף י"א.), as elucidated by Rashi (שם בד"ה ואתנה), understands that the service of the Leviyim referred to here is their שירה, their singing, which accompanied the offering of the Korbanos. Indeed, the Gemara quotes one opinion which holds that the song of the Leviyim is so essential to the Avodah of the Korbanos that a Korban brought without any accompanying song is invalid. The Yerushalmi in Pesachim (פרק ד' משנה א', דף כ"ה.-כ"ה:) quotes a view which, citing our Posuk, holds as well that the absence of singing with a Korban invalidates the Korban; the Gemara in Taanis (דף כ"ז.) presents this view with a slight variation, quoting that it is the accompaniment of musical instruments that is absolutely necessary with a Korban, and not the singing per se'. Although the Halacha does not appear to go so far as to disqualify a Korban without שירה, we may nonetheless conclude that singing and musical instruments were an integral part of the Avodah in the Beis HaMikdash, as seen as well by the importance placed on reciting the proper שירה by the Mishnah in Rosh HaShanah (דף ל:, ועיין שם בתוד"ה ונתקלקלו).
The Gemara in Gittin (דף ז.) indicates that nowadays, in the absence of the Beis HaMikdash, music is prohibited, and the prohibition includes both singing and the playing of musical instruments. Although the Gemara there does not explicitly state the reason for this prohibition, the Rambam (פרק ה' מהל' תעניות הלכה י"ד) clearly understands that this prohibition was instituted following the Churban of the (second) Beis HaMikdash as a sign of mourning, noting that it is forbidden as well to listen to music. The Mishnah in Sotah (דף מ"ח.) states that once the Sanhedrin ceased to function, some time before the actual Churban, music at בית המשתאות, party and banquet halls where people drink, ceased as well. The Yerushalmi (שם פרק ט' הלכה י"ב, דף מ"ה.) explains that as long as the Sanhedrin functioned, people feared them and would not sing vulgar songs at celebrations. But once the Sanhedrin was no longer in authority, some people began to sing vulgar songs at affairs, and, apparently, the practice therefore developed then to avoid all singing. The Meiri, in his commentary on the Mishnah in Sotah (שם), likewise seems to understand that this ban developed because excessive singing at parties would lead to other inappropriate activities; this also seems to be the position of the Rif in Berachos (דף כ"א: בדפיו). The Rambam, though, as mentioned above, states clearly that the issue is one of mourning the absence of the Beis HaMikdash; it is possible that initially a ban on music developed after the Sanhedrin ceased to function in order to prevent vulgarity, and then, after the Churban, the Rabbanan formalized this practice by outlawing music in order express the feelings of mourning as well.
The simple reading of this Mishnah in Sotah (שם) implies that the ban on musical expression was intended for when it would be associated with the drinking of wine; this also seems to be the context in which the discussion of the Gemara in Gittin (שם) was understood by Rashi (שם בד"ה זמרא). The aforementioned Rambam (שם) too states that the ban on music applies specifically when it is accompanied by the drinking of wine, although he mentions this point only regarding vocal music, implying that instrumental music is forbidden even when no wine drinking is involved. Tosafos in Gittin (שם בד"ה זמרא), however, suggest that one ought to be strict and avoid all music listened to with regularity, even if it's not sung or played along with the drinking of wine. The Tur (אורח חיים סימן תק"ס) quotes this Tosafos prohibiting music if listened to regularly, even without drinking, but then notes an apparent contradiction in the view of the Rambam. In the Mishneh Torah (שם), as cited above, he forbids instrumental music under all circumstances, and vocal music only with wine. But in a Teshuvah (תשובות הרמב"ם הוצאת בלאו חלק ב' סימן רכ"ד), he forbids vocal music even without wine. It is possible that the difference lies in the nature of the song and its words; the Bach (שם בד"ה ומ"ש אבל) offers a different solution. The Shulchan Aruch (שם סעיף ג'), apparently following the Rambam in the Mishneh Torah (שם), prohibits musical instruments entirely because of the Churban, and restricts vocal music if accompanied by drinking wine, while the Ramo (שם), writes that the prohibition is only for one who regularly listens to music or if one is at a party (where people drink wine), in which case, as the Mishnah Berurah (שם ס"ק י"ב) notes, if wine is involved, singing is definitely forbidden under all conditions.
In his Mishneh Torah (שם), the Rambam then adds that it has become customary for Jews to sing songs of praise or thanks to Hashem, and other such songs, even over wine. The Maggid Mishneh (שם) quotes that such is also the view of Rav Hai Gaon, who explains that the entire ban was only on love songs and the like that people sing to each other, but songs which praise or recount the kindness of Hashem are customarily sung at weddings and parties with great happiness. The Rif cited above (שם) also quotes from the Geonim that nobody objects to songs of praise and thanks to Hashem, and the Meiri mentioned earlier clearly holds that songs of praise to Hashem are allowed, as does the Tur (שם), who allows them even with wine. These views are easier to understand if we assume that the entire ban on musical expression was designed to avoid the singing of lewd and vulgar songs; "Jewish music" praising Hashem would thus not be included. But if music was prohibited as a sign of mourning after the Churban, all music should be disallowed, unless we assume that certain kinds of songs were never intended to be eliminated from Jewish use. The aforementioned Tosafos in Gittin (שם), for example, says that if the song is part of the fulfillment of a Mitzvah, like gladdening the Chassan and Kallah at a wedding, it is permissible. The Shulchan Aruch (שם), again following the Rambam (שם), writes that it is customary to sing words of praise or songs of thanks to Hashem even with wine; the Ramo (שם), following Tosafos in Gittin (שם), adds that all is permissible for the sake of a Mitzvah. Elsewhere in the Shulchan Aruch (אורח חיים סימן של"ח סעיף ב'), the Mechaber seems to agree to this as well.
It is interesting to note, however, that in the latter part of the nineteenth century, the rabbis of Yerushalayim enacted a ban for their community on the use of instrumental music even at weddings. This decision seems to be based on the fact that the Rambam (שם) never specifically allows instrumental music at weddings or for a Mitzvah, implying that he holds that it is always prohibited. Perhaps specifically in Yerushalayim, where the Churban is most noticeable, a decision was made (and is still observed in parts of the city) to accept a stricter opinion as an additional sign of mourning. Elsewhere, however, the practice is to permit instrumental music at weddings and other Mitzvah-oriented gatherings, as allowed by the Ramo (שם).
Rav Moshe Feinstein (שו"ת אגרות משה או"ח חלק א' סימן קס"ו) rules that singing vocally is permitted unless done over wine or with great regularity, noting that only the Rambam in his Teshuvah cited above prohibits this, although he says that one who wishes to be strict and avoid all singing may do so. Instrumental music, however, is always forbidden unless it is for a D'var Mitzvah; he then discusses how broadly to define a D'var Mitzvah. He also writes that over the radio, one may listen to singing but not to instrumental music. Rav Ovadyah Yosef (שו"ת יחוה דעת חלק א' סימן מ"ה), however, quotes others who believe that both vocal and instrumental music may be listened to from a radio or a tape recorder. He also quotes that listening to music for its intellectual and aesthetic value, and not simply for fun, may be permissible; it has been reported that HaRav Joseph B. Soloveitchik agreed to this. The Mishnah Berurah in his Shaar HaTziyun (או"ח שם אות כ"ה) also allows singing a child to sleep, although he stresses that even such songs should be appropriate. There seems to be no question that listening to or singing certain kinds of songs, especially ones with lewd or vulgar lyrics and themes, is prohibited.