If your dog is getting old, it becomes weaker and less agile in its age just as humans do. As a dog ages, their joints become less flexible and the strength in their muscles begins to deteriorate, their control of their limbs also becomes less precise. If your dogs legs giving way are a sign of old age, unfortunately there is no treatment. In these cases you will just have to be more aware of your dog and keep an eye on him. If your dog has a fall, give him a quick check over to make sure he has not caused himself any damage. Keep your dog exercising to avoid his joints seizing up but do not push him too far if he is starting to look like he is suffering.
Another common cause of back legs being weak and giving way in dogs is arthritis. This is also more common in older dogs but is something that can be relieved. A vet will be able to prescribe your dog with NSAIDs, these are a powerful treatment for arthritis but not suitable for all dogs. Improving your dogs lifestyle can help reduce the symptoms of arthritis. Managing his weight will take some of the pressure off his joints, this can be done with dieting and exercise. Exercise will also help keep his joints a bit suppler.