The choice of længe/lang tid is depending on the role the word plays in respect to the verb.
Længe and i lang tid are time adverbs and serve as such in a phrase:
De ventede længe/i lang tid - They waited for a long time.
Det varede aldrig længe/lang tid, før han mødte den eneste ene - It never took him long to meet the one and only
Lang tid can either serve as time adverb or as subject/object - depending on the nature of the verb:
De ventede lang tid på at koalaen skulle vågne - They waited a long time for the koala to wake up (adverb)
Det tog ret lang tid at vaske elefanten - It took some time to wash the elefant (object)
Der gik lang tid med at massere giraffens ømme hals - It took a long time to massage the giraffe's sore neck (semantic subject)
Hence, the problem is mainly not to use længe as object - in most cases lang tid will be OK although længe collocates with certain verbs. Sometimes, there is a difference in meaning when you can use both længe and i lang tid.
Counting hours/minutes etc. will call for lang tid, whereas the late ending of something will favour længe