My horse used to do that. Most of the time it's just them not actually wanting to leave the barn and hoping that maybe they'll get away with it and get to turn around and go home. For me and my gelding, whenever he started that I'd kick him up to a trot and make him move forward so he had more to think about and was too busy to keep doing such silliness. I call it his 'drunk walk.' Be strict and cut him off with leg and rein if necessary when he tries it and make him move forward with purpose. It is a pretty common horse behavior to have to deal with.