YouTube didn't get sued early on because it didn't have any money to sue. After it was acquired, it was sued, and the current system is the agreement that Google and the rights holders came to (which has been incredibly profitable for both sides).
There is a load of pirated content on there, but YouTube have successfully cowed every creator with a decent-sized audience into fearing more than a second or two of copyrighted music appearing in their videos.
Is it legal? I don't know. I guess they have the fig-leaf of taking down copyrighted content when asked.
Fig-leaf not withstanding, if Google (YouTube's owner) didn't have such deep pockets I'd be amazed they didn't get sued in to oblivion like Napster.