The first differentiation in human embryogenesis is from early blastomeres into trophoblast, which forms the outer layer of the blastocyst, and inner cell mass (ICM). It may be unsurprising then that cells on the inside of the 8-16 cell stage morula
differentiate into ICM whereas those on the outside differentiate into trophoblast. However it is currently unclear whether this is a causal relationship (the inside-outside model) or whether blastocyst patterning is set-up earlier in cleavage (such as in the apicobasal polarity model) or if it is some combination of the two.
You were correct in imagining that tissue patterning is determined by chemical substances: a very broad class called morphogens. Cells don't necessarily count how many times they have divided but cell division can certainly affect morphogen concentration and localisation. I suppose in some sense you could describe this as a clock. I wouldn't though, especially because mammalian cleavage is asynchronous.
One line of studies showed that a transcription factor called Tead4 specifies trophoblast$^{ref}$. It was then shown that Tead4 can be regulated by cell-cell contact (mediated by Hippo signalling) $^{ref}$. They hypothesised that the magnitude of cell-cell contact could provide a mechanism by which Tead4 localisation could be controlled in presumptive trophoblast and ICM. This could explain why the cells have different fates because inner cells are completely surrounded (more cell junctions) whereas outer cells are largely exposed to the medium. Note that I only provided that as an example of a possible mechanism. It is not confirmed and, as with any model, the real picture is incredibly more complex and likely involves many factors.
I'm reluctant to go into too much detail on this because you're asking about a complex process that is still under active study and I'm unclear as to how much information you actually want. A book could be written on this subject alone, and many review papers have been published. Here's a recent one:
Development: Do Mouse Embryos Play Dice?
I suggest you read that and then if you have further, more specific questions, we can try and answer them for you.