something that you say when you have been asked about something that needs a long explanation 'So what were you doing in Nick's garage at three o'clock in the morning?' 'Ah, thereby hangs a tale.'
That detail or incident reminds one of another story, as in So he went without supper, but thereby hangs a tale. This expression, embodying the pun on tail and tale, was used by Shakespeare in at least four of his plays and presumably was well known before that. [1500s]