Giving way of the knee can be primary or secondary.
The joint actually gives way or goes out of joint. This is caused by a ligament deficiency such as an ACL tear. Typically the actual giving way sensation is not painful but the knee hurts afterwards and may swell. The swelling is bleeding into the knee from damage to one of the internal structures. Repeated giving way damages the menisci and chondral surfaces of the knee and leads to arthritis.
Since this is a mechanical problem surgery is almost always required to fix the problem. Most commonly this is in the form of an ACL reconstruction but can also be from a PCL injury, a collateral ligament injury, patellofemoral instability or a posterolateral corner injury.
This occurs when something causes pain within the joint. The quadriceps muscles wants to reduce the compressive force on the painful joint so it relaxes involuntarily. This allows the leg to buckle and gives the sensation that the knee has given way. There is no structural cause of the giving way in this situation and if you remove the cause of the pain then the knee will no longer give way. The cause of the pain may be a meniscal tear, loose body, arthritis, or synovitis. The giving way in this situation is generally not causing further damage to the knee.
There are times when the kneecap going out of joint will be perceived as giving way of the knee. This is in fact patellofemoral instability. The bones are the most important structures keeping the kneecap in place but the thigh muscles are also important in this function
A careful history and clinical examination will help differentiate between primary and secondary instability and then appropriate treatment can be instituted. In most cases arthroscopic knee surgery is required to treat giving way.