Let the number of sheep be denoted by the letter "S".
Let the number of chicken be denoted by the letter "C".
From the given information about the number of heads (headcount) it is clear that:
.................(Equation 1)
We know that 1 Sheep has 4 legs and 1 chicken has 2 legs. Therefore, "S" sheep will have 4S legs and "C" chickens will have 2C legs.
Therefore, from the information on legs we have:

Dividing both sides by 2 we get:
.............(Equation 2)
Now, if we subtract (Equation 1) from (Equation 2) we will get:

or, 
Plugging in this in (Equation 1) we get:

or 
Thus the number of sheep is 16 and the number of chicken is 39.