A 'bee', in this sense - so-called after the social, hard-working nature of the insect itself - was a gathering of people, generally neighbours in the USA, to carry out some common task. Thus, you could have quilting-bees, apple-bees (to bring in the fruit from each other's orchards) and so on. A spelling-bee is just such a gathering to compete over who can spell best.