What came before and after? Why did communities stop using them as punishment?
In Australia the stocks were a minor judicial punishment usually located at gaols, prisons or outside courthouses (possibly what OP means by "in the town square") They were in use in the different colonies, notably in Tasmania, and while they were humiliating and possibly painful, the journalist, newspaper proprietor, and mayor of Launceston, Henry Button, called the experience "petty ridicule...endured in silence".
Other judicial punishments available included hanging, imprisonment, flogging, being placed in leg irons, and fines. The stocks did not attach much notice as a punishment and seem to have been considered trivial.
George E. Boxall mentions being present at the Geelong Courthouse when the bushrangers Captain Melville and Roberts, were arraigned (late December 1852) and mentions, as a curiosity, that he saw the last stocks in Victoria located there, the ones at Melbourne courthouse having been destroyed some years earlier, and that the Geelong stocks were broken up for firewood two years later.
So, in Australia, the stocks went out of use by the 1850s. They were not a significant form of punishment. There were alternate punishments available and the courts gradually stopped using them.
There appear to be a set of stocks still in existence at the Round House gaol in Fremantle, Western Australia as there are occasional photos posted of tourists in them.
References:
History of the Australian Bushrangers by George E. Boxall
Australian Dictionary of Biography pub. by Australian National University
Becoming a Penal Colony (Extracts from Becoming Tasmania) by Terry Newman