In the cart
From the practice of taking prisoners for punishment or to their execution in carts. This is alluded to by John Dryden in his play The Loyal Brother, 1682: "Like thief and parson in a Tyburn-cart". Tyburn, with its infamous Tyburn-Tree, was London's principal place of public executions between the 12th and 18th centuries. Victims were transported to the gallows in a cart, which was also used as the means of execution - by attaching the noose and then driving the cart away. Francis Grose, in his 1811 edition of The Dictionary of the Vulgar Tongue, refers to this in his definition of an innovation called the New Drop:
NEW DROP. The scaffold used at Newgate for hanging of criminals; which dropping down, leaves them suspended. By this improvement, the use of that vulgar vehicle, a cart, is entirely left off.
Carts were used for punishment in less final ways than hanging - in a practise called being whipped at the cart's tail (or arse). This was also explained by Grose in the same work:
"Persons guilty of petty larceny are frequently sentenced to be tied to the tail of a cart, and whipped by the common executioner, for a certain distance."
This was well enough known by 1811 for Grose to record several synonyms for it:
"To SHOVE THE TUMBLER - To be whipped at the cart's tail."
"STRONG MAN. To play the part of the strong man, i.e. to push the cart and horses too; to be whipt at the cart's tail."
"AIR AND EXERCISE. He has had air and exercise, i.e. he has been whipped at the cart's tail; or, as it is generally, though more vulgarly, expressed, at the cart's a-se."
He also records the practise of carting, which appears to be a form of mobile naming and shaming:
"CARTING. The punishment formerly inflicted on bawds, who were placed in a tumbrel or cart, and led through a town, that their persons might be known."
Whatever the miscreant's destination the experience of being in the cart can't have been a happy one and it isn't difficult to see how it came to mean in trouble.
By 1889 the term was also being used in a figurative sense; for example, this piece from The Evening Standard from June that year:
"In two races ... Sir George Chetwynd - to use a vulgarism - had been 'put in the cart' by his jockey."