Actually, it is quite a grey area. Fanfic/spec scripts could be considered derivative works and therefore infringe on the copyright on the original work, but there are provisions for fair use and parody.
And poorly understood! George Martin of Game of Thrones writes that he's against fanfic. Because if he allows it, he can lose his copyright. Then he goes on about Tarzan, and how since people made unauthorized movies on the theme, the author of Tarzan dies poor.
Plus the standard "I made them no one else can touch", which is a valid personal feeling, but little basis for law.
In practice, companies do not tend to go after these people since there is the risk of an uproar and it does no harm to the company typically. In addition, the risk of an expensive lawsuit is usually not worthwhile on the companies' end, especially given the pros/cons