I'm not a vet , so I can't tell you the exact reason why your dogs legs are giving out on him. The only person qualified to tell you why and what needs to be done to correct the problem, is a Veterinarian.
You need to take your dog to the vet and have him/her examined and x-rayed , to find out exactly what is causing his/her back legs to give out. There are so many different possibilities.
Luxating Patella (Slipping Kneecap or Trick Knee), can cause a dogs legs to give out, if it's severe enough. Whatever if may be his Hip, Back, Legs etc., you need to find out and get it treated. It's painful and very uncomfortable for your dog.
Don't let this go. It needs to be checked out, the sooner the batter before it becomes a permanent problem that can't be fixed, if you wait too long to get him/her to a vet.
You need to get your poor dog some pain relief. In the meantime, don't let him/her run around or jump on or off anything, because he/she will injure themselves more.