While the duty to perform a contract is usually a "strict liability" obligation, the doctrine of impossibility or impracticability of performance invalidates contracts that are impracticable to perform for unforeseeable reasons.
Example 1: Say I'm given a contract to paint a house, and a few days
before I am assigned to paint it, the house is hit by lightning and
burns down, making my work impossible to complete. Do I still get
paid?
The contract is invalidated and you don't get paid for anything more than the fair market value of the work you actually did under a quantum meruit (a.k.a. unjust enrichment a.k.a. restitution) theory.
Example 2: Say I'm given a single contract to paint two houses. I
paint the first house, then lightning strikes the second and it burns
down, so I can't paint the second house. Do I still get paid the value
of the entire contract?
No. The contract would likely be found to be "severable" and the portion related to the second house would be invalidated on the grounds of impossibility. You would only be paid for the work you actually do.