We don't know the exact origin of this expression. Some people think it had something to do with working "the pit" in sawmills, which was an annoying job, some people think it refers to armpits, others think it might have to do with another expression "depths of despair".
"The pits" as in "This is the pits" is usually used to refer to an undesirable situation.