At the trans the cable goes through a round hole where two plastic retainers spread out to retain it in the round hole. There is a "U" shaped metal clip that inserts behind these plastic retainers to hold them out so that cable cannot come out of the hole and bounce around. Check to be sure the cable is inserted through the hole correctly and the clip is not missing. If the clip is missing and the plastic retainers have failed the cable may be allowing the end of the cable to come off the shift lever ball stud as you had mentioned. If everything is ok there, the shift cable ball stud retainer may be worn or the ball stud at shift linkage could also be worn. Usually a screwdriver or pry bar is needed to take the cable off the linkage. With this known try pulling the cable off by hand to verify is there is a lot of tension holding the cable to the stud.