Well, technically it wasn’t legal to sell a child in New England in the 1830s. However, a parent could apprentice a child to a craftsperson or farmer, giving the master nearly complete control over the child under the apprenticeship agreement. This arrangement was also referred to as an indenture; hence such children were sometimes called indentured servants.
When Mr. Stocking offers to “buy” Billy from Hugh Fogarty, he’s not literally buying Hugh’s child, but offering to take Billy on as an apprentice or indentured servant. Such arrangements might happen if  parents were too impoverished to take care of their children. Parents also apprenticed their children to get them trained for a trade or craft. A parent might also hire a child out as a worker for day wages; any pay the child received would legally belong to the child’s parent or guardian.