There are several devices that can cause the computer to wake up or not sleep in the first place. The keyboard, the mouse and even the network adapter can all cause the problem. To fix this proceed as follows.
Get to the control panel by typing "Control panel" into the Windows search box in Windows 10 or 8. (In Windows 7 left click on the start menu and then on "Control panel"). From the control panel select "Device manager" then proceed as follows.
Check every one of the items shown here in red as any of these can wake up the computer or prevent it from sleeping.
Click the little icon by the side of each one to expand the menu like this. Now right click and then click on "Properties" Look for a tab that's labeled "Power Management" and click on that. If there is no such tab then ignore that device. Take the tick out of the box that says "Allow this device to wake the computer" then click OK.
Be a little bit careful at this point because if you use this trick on keyboard and mouse you may find that you can't wake the computer when you want to! All of these fixes should be tried one at a time in order to establish which is actually causing the problem. The main culprits are keyboard, mouse and network adapter.