This expression that means to tell a long, engaging story dates back to the early 19th century. Its source is the yarn lofts at seaports, where yarn was spun daily to supply ships with rope. Rope was a big commodity of mercantile Europe. Huge quantities were needed on trading vessels. Each rope was made up of segments called yarns. Spinning took a long time and required groups of three to 10 men at the wheels. Work was monotonous, so it's no surprise that the yarn loft was known for storytelling.
This term used to describe anything excellent originally referred to 19th-century sailing vessels. Lloyd's of London, an insurance firm, used the term to record the condition of merchant vessels they insured. Ships or hulls were graded by letter; cargoes, anchors, rigging, etc., by number. Any ship classified as A-1 was considered in perfect condition. Today, A-1 signifies anything shipshape. Credit for expanding the usage goes to Charles Dickens, who gave the phrase a non-nautical sense in "The Pickwick Papers" (1837).
SOURCES: 'The Encyclopedia of Word and Phrase Origins,' by Robert Hendrickson; 'Why You Say It,' by Webb Garrison; 'Dictionary of Word Origins,' by Jordan Almond; 'World Book Dictionary'; 'A Browser's Dictionary,' by John Ciardi; 'Brewer's Dictionary of Phrase and Fable,' by Ivor Evans; 'Dictionary of Contemporary American Usage,' by B. and C. Evans; 'The Morris Dictionary of Word and Phrase Origins,' by W. and M. Morris.