A story written by a member of an existent intellectual property's fandom, usually written in that property's universe, and including characters, places and events from that universe. However, sometimes, this is not the case.
When fanfiction is good
Sometimes fanfiction can be good: An interesting story in an existing setting, An alternate universe the fan writer wishes to explore, Exposition or lore fashioned into an easier consumed narrative, and more. Because of this, it is possible for fanfiction to be surprisingly well written. The blessed individuals that grace /tg/ threads with this rare treasure are affectionately dubbed Writefags.
BAD fanfictions are usually stories that are little more than the writer's sexual fantasies put to paper (E.G. a certain story involving gay orks or making your waifu fantasies with Elesh Norn come true, And that's barely the tip of the iceberg.) These are called FapFictions, and they are unnervingly prolific.