I'm assuming the question you're asking is how is a distribution from an S corporation handled?
Assume for sake of argument, your basis in the S corp stock is $10,000. You take a distribution of $5,000. That is treated as non-taxable. However, final determination of this is made at the end of the year.
As of the year-end, you take the beginning balance of the stock basis, add to it, the items of income, separately stated items etc.
Subtract any losses and separately stated items.
Then subtract distributions.
If the basis in stock is zero or above, then the distribution is tax free. (stock basis can never be negative)
If however, an amount of the distribution exceeds the basis in stock, then the excess is taxed as a capital gain.
Thus if beginning basis in stock is $10,000, and items of income, expenses, losses, separately stated items reduces the basis to say $4,000 and you take a $5,000 distribution. Your basis in stock is zero and $1,000 is a capital gain.
Here is a more comprehensive example