WD40 works for a rusted chain. It is not a good lube and will wash off easily, so add some thicker lube when it is running freely.
If the transmission is worn, you may need to replace the chain and freewheel (rear cogs). For occasional use you can make do with a worn tranmission but for regular or extended cycling it is worth starting with a new, unworn setup. A new chain on worn cogs will slip and the chain will wear out more quickly.