A temple is a building that can be built in friendly provinces. Most nations pay 400g for a temple, although there are some exceptions; they can be built by any sacred commander.
Temples perform two main functions:
They spread dominion. Each temple produces one [temple check] per turn, which will produce one friendly candle or possibly remove one enemy candle in the same province or a nearby one. (Exception: EA and LA Mictlan)
They unlock recruitment. Sacred units can only be recruited in provinces with a temple. If your nation has the ability to recruit out-of-cap sacred units (rare) or out-of-cap sacred mages (common), you will want to build temples in most/all of your forts.
There are a few other things temples do:
They increase dominion strength. Every five temples built by a nation increase the dominion strength (chosen in pretender design) by one. (In disciples games, the number required is five times the number of nations on the team.)
They provide a site for blood sacrifices. Some nations have the ability to perform blood sacrifices at temples, in which a priest can sacrifice a number of blood slaves equal to their priest level to create that many additional temple checks.
Unlike laboratories, which are generic and stay in a province when it is conquered, temples are nation-specific and are destroyed when a province is conquered, with one or two national exceptions.
Nations with sacred mages will generally build a temple in each fort to allow for recruitment.
Otherwise, how many temples you build will depend on how strongly you need to push your dominion. If it is very important to keep enemy dominion out of your provinces, or shove your dominion into enemy provinces, then more temples can do this.