How do lilies sense day and night and open and close their flowers?
The three main cues for flower opening/closing used by plants are temperature, light and humidity (van Doorn &amp; van Meteren 2003, freely available pdf), with the first two being most common. Plants with daily opening and closing of flowers can be divided into nocturnal (open at night) and diurnal (open at day). There exists several different mechanisms for how flowers open and close, and these include e.g. reversible expansion and contraction of cells by changes in water balance and differential growth of cells due to temperature (in either the petals themselves or cells at the base of flowers). See the previously referred paper for examples.

The evolutionary advantages and molecular mechanisms (along with the circadian clock of plants) behind opening and closing of flowers are open research fields. Several evolutionary hypothesis exists for explaining different flowering strategies, and these often deal with efficient pollination (close when good pollinators are absent), protection against frost, conservation of resources (e.g. water) or predator/pathogen defence.

I do not know what the specific mechanism in lilies is, and it can possibly/probably differ between different species.