The tumblers in locks are spring loaded to go back to 'locked' position when key is removed, the key indentations push them into a position where the barrel can turn. Now because the key has to pass all the tumblers ( pins or levers) on its way in and pit of the lock, if the tumblers are not free to move they can ' hang up' when they are pushed up by a 'peak' on the key and not be able to move down into a 'trough' when they should, WD40 is infamous for dissolving out any existing lubrication ( which is why it makes a good cleaner) and replacing it with a sticky lacquer ( it was designed to get rid of moisture WD stands for Water Dispersant) and leave a sticky protective film behind, it is a corrosion pre enter... You should really use dry lubricants in locks like powdered graphite but a good quality PTFE ( Teflon ) based lubricant which will evaporate and leave a very slippery inert film behind is pretty good ( 3in1 advanced PTFE lubricant, I have used it on locks that are out in all weather and between 5 and 10 years later they still work perfectly ( without any topping up of lubrication).
As sparky Paul mentioned WD40 is not a lubricant and can cause more problems than it solves, it was very well marketed though as the answer to everything...
if you regularly use the key to operate the locks the tumblers get worn and even if well lubed do not move to proper place to release the barrel, also keys can wear. Try some decent PTFE lube, but you may well have to get replacement barrels with keys..