It's just about popularity:
Senbatsu (Most popular girls)
Undergirls (Less popular than Senbatsu)
Theatre Girls (Less popular than Undergirls)
I think they got the name Theater Girls because the main activity of an AKB member is to perform on the theatre, and if they are not popular they don't get extra jobs to do, so the only thing they do is to perform at the theatre
In the rankings:
* 21 members from #1 to #21 are selected to be on the a-side song, they perform under the name of AKB48.
* 8 members from #22 to #30 are selected to be on the b-side song, they perform under the name of Undergirls.