The name Gothic (in terms of books, stories, and music) comes from the Gothic genre of fiction. Things such as Dracula, Poe, Frankenstein. These were nicknamed Gothic Fiction or Gothic Horror because they took place often in Gothic Buildings. Gothic Rock was named for its moody and dark style comparing it to the Gothic horror or Gothic Fiction. One of the first Goth Rock songs "Bela Lugosi's Dead" references the actor in many Gothic Horror films.
As for the name Gothic Architecture, this came as an early insult to the style (that started around the 14th/15th century) comparing it to the Goths who had restored buildings in Rome and replaced it with their own style. The early Gothic churches were considered "barbarous".
The Goth subculture ultimately derives its name from the Gothic literary movement of the 18th and 19th century, which was a darker offshoot of the Romantic movement (think *Frankenstein*, *The Legend of Sleepy Hollow*, and the works of Edgar Allen Poe).
The literary genre takes its name from Gothic art and architecture from the middle ages, since the stories were stereotypically set in castles and other medieval buildings.
That name itself originated as a pejorative coined during the Renaissance (the style was in its time called "French") and was compared to the Goths, barbarous Germanic invaders who supposedly destroyed Rome and erected their "Gothic" architecture across Northern Europe before the resurgence of classical style during the Renaissance. Basically, it was meant as a synonym for "barbarian."