I was thinking of various monsters from popular fiction (specifically the 5 nights at freddy's robots), and I wondered. is it possible to have an undead construct? How do templates work in the game? and how does it affect CR?
Construct and Undead are types, not templates. A creature can only have one type, though it may have its previous types as subtypes. So an 'undead construct' of the kind you are talking about would probably be type Undead(Augmented Construct). This means they are functionally undead and not constructs, despite having been constructs in the past. What exactly happens depends on the template you use to turn the Construct undead. This template would need to be homebrewed because there are no existing templates that convert constructs into undead.
Templates stack. After each template, you have a new monster, which you could then apply an additional template to. You must apply them in an order, and that order usually matters a lot. Some combinations may only be possible with a certain order, because of prerequisites. For example, you can have a Half-Fiend Half-Dragon Half-Celestial, but not a Half-Dragon Half-Celestial Half-Fiend.
CR adjustments are listed with templates. They also stack. CR is not a good measurement of power, and this will require adjudication as to the appropriate CR of the finished creature if you want it to be a more useful metric.