Asked and answered on Aviation.SE.
Summary: They're required by US federal aviation regulations. The apparent rationale is: a desperate, or unscrupulous, smoker might (illegally) light a cigarette in the lavatory. If they do, it is good for there to be somewhere safe for them to put the cigarette butt. Otherwise, if they don't see anywhere else to put it, they might put it in the trash receptacle, which is full of combustible paper and could start a dangerous fire.
There is speculation that the 1973 crash of Varig Flight 820 may have been caused by just such a fire.