According to the OED, it comes from a 19th joke story from the Indiana and Kentucky border area about shirt making. The earliest cited quote in the OED is from 1855 and recounts the story:
1855 New Albany (Indiana) Daily Ledger 30 Jan. 1/4 ‘The Judge's Big Shirt’... What a silly, stupid woman! I told her to get just enough to make three shirts; instead of making three, she has put the whole nine yards into one shirt!]
An early 20th century variant was "the whole six yards."
The first four quotations cited by the OED, up through 1956, are all from Indiana and Kentucky, though after the initial citation the metaphor was applied to other activities, so:
1908 Mitchell (Indiana) Commerc. 4 June 3/5 Roscoe went fishing and has a big story to tell... He will catch some unsuspecting individual some of these days and give him the whole nine yards.