I went to a panel at a convention recently on the legalities of fanfic. I've written fanfic in the past, and will probably read more in the future. And I look forward to the day when fans of my own writing like it enough to write fanfic based on it. So I was looking forward to the panel discussion.
Unfortunately, it seems none of the people on the panel knew anything about the actual legalities of fanfic. The discussion was mostly, "Well, I like fanfic, so I think it's legal," and "I think fanfic is stealing, so I think it's illegal," and back and forth from there, based on what the panelists thought and felt about it.
In the real world, what any individual thinks or feels about fanfic is completely irrelevant to the question. The moderator made it clear that contributions from the audience had to be 1) only questions, and 2) tweet-length or less, so I didn't bother trying to throw any actual facts into the mix at that time.
In actuality, 1) nobody knows for sure whether fanfic is legal or illegal because it's never been litigated, and 2) if it is, most likely only some of it is, and the rest is not.
The fact is, the Fair Use rules are purposely ambiguous.
Whether you're making money with your derivative work is one factor, and the fanfic that gets written and posted to people’s journals, tumblrs, on Facebook, on personal web sites, and on archives like AO3 and fanfiction dot net for anyone to read for free satisfy that particular factor. That's not the only point, however.
Another point is whether the work is used as criticism or parody, and some fanfic clearly is. I know a popular fanfic writer who thinks Dumbledore is an evil, sociopathic villain, and a dark lord himself if you look at what he did and what he allowed to happen over the course of the books. She also thinks Ron Weasley is a selfish, lazy, jealous git who turned against Harry at the drop of a hat, multiple times, generally an asshole. Pretty much all her HP fanfic reflects these two beliefs, in how she writes these characters. You could make a good argument that all of her HP fanfic is criticism of Rowling's books. And a lot of fics are clearly parodies of the original works, and therefore would be no more infringement than Bored of the Rings was an infringement on Lord of the Rings. (Believe me, if the Tolkien estate could've sued National Lampoon for that, they would've; they’re notoriously litigious.) Many other fics don't satisfy the criticism or parody points, so it's possible that the critical/parody fics are completely legal while the others are not.
Another factor is whether the work is transformative. Did the fan writer clearly change a significant part of the IP to create something new? In this case, people who write further-adventures type stories, where Kirk and Spock beam down to a planet and stop two peoples from going to war against each other, are more likely to be in legal peril, while the people who write alternate universe stories where, for example, Kirk and Spock are each members of a different species of dragon, and their dragon clans have been feuding for a thousand years, but the guys meet and get together and bring peace to their clans, Romeo-and-Juliet style but without the tragic ending, are more likely to be in the legal clear.
And another factor is whether the derivative work is likely to negatively impact the earning potential of the original IP. If someone is writing My Little Pony fanfic that's just so wonderful and awesome that Pony fans stop watching the show (and buying the merch) and only read that fanfic, then the MLP IP holder would have a reason to sue, and might win. Although I don't know of many IP holders, either of books/stories or TV/movies, who'd be willing to stand up in court and say that the fanfic of their work is so much better than the work itself that it's cutting into their revenue. :)
And in actuality, if anything it's the opposite. Rabid fans of a property (and you have to be a fairly rabid fan to put in the work and effort of writing fanfic) drag their friends into the fandom by the ear, shoving books or boxed-set DVDs into their hands and practically forcing them to get into the original so that they'll understand the fanfic.
And some people read the fanfic first, then go looking for the original. I've done that a couple of times, in fact. I watched the first couple of episodes of Stargate: Atlantis, went "Meh," and wandered away. A couple of years later, a fanfic writer I like a lot started writing Atlantis fic, and I read it. I enjoyed it quite a bit, went back to the show, and (reading it through slash goggles that I won't apologize for, plus with the extra world- and character-building the fan writers had done in my mind) I enjoyed the show much more. I ended up buying all five seasons on DVD. I also read a really great fic based on the movie Blackhawk Down, then watched the movie, which wasn't my usual thing; I'd never have watched it if I hadn't read the fic first. This isn't the normal way people get into a fandom, but I'm not the only one who's come in through the side door this way. Fanfic and its writers/fans increase engagement with the canon IP, rather than decrease it. Creators who object to fanfic are, IMO, shooting themselves in the foot, and then refusing even a bandage out of sheer cussedness.
The fact is, nobody knows whether "fanfic" is illegal or not. There've been people, usually IP owners of one sort or another, who've very firmly insisted for a long time that it definitely IS, and who've gotten very nasty with their fans who write fanfic. This is stupid, but hey, if someone wants to piss in their own soup pot, that's their choice. For myself, I'd be delighted to find fanfic written about any of my published work.