WD40 ("WD" stands for "water displacement") is primarily a solvent, penetrant, degreaser and water displacement solution. You can use it to free up gummed up parts on your bike, but you should follow that up with a proper lubricant for the long haul. It has lubricating properties in the sense that it is a liquid, but it doesn't stick around very long and that's not good, especially on things like your chain or other spinning parts.
If parts are gummed up, rusted or slightly corroded, WD40 can be useful to get them freed up and it will have a wicking action to help get some proper longer lasting lub into where it needs to go. But if things are not gummed up, rusted or slightly corroded, there's not much point in using WD40 unless you are really in a temporary jam and things are totally dried up with no lube at all present.
For your chain, use a proper bike chain lube and follow the recommended directions for best results. You could do a lot worse than using Tri-Flow and it is readily available, but there are many different products available and some address various conditions better than others (wet, dry, dusty, etc.). Tri-Flow, DumondeTech, Boeshield T-9, Finish Line, Pedros, White Lightning, ProGold ProLink, Rock N Roll & Phil Wood are some of the more popular lube brands that would be better than WD40 to one degree or another.
When applying lube to things like derailleurs, use a less-is-more approach so long as it gets applied right where it needs to go. Use the small tube many come with to get a couple drops right on target rather than a spray. If you wipe away excess in places it doesn't need to be, it will not attract and/or hold as much dirt and dust.