A door, that does not stay in the position it was left in, is out of plumb. That means that the pins in the hinges are not located plumb with each other. Sometimes this is matter of loose screws on a hinge. Check that first. On that note, a door that swings close usually means the bottom hinge is loose and the door that swings open usually means the top hinge is loose. If the hinges are both tight, then the door was never hung correctly to begin with. This can be fixed by moving the hinges, but moving the hinges might cause other problems like the door won't close flush anymore. Start by putting a level on the hinge side of the door when the door is open perpendicular to the wall (the part that the hinges screw into). You may notice that the top of the door leans towards the jamb. If it is not plumb then you can move a hinge or both. Getting the two hinges in plumb with each other means moving the top hinge out or the bottom hinge in on the door jamb. If it is plumb with the door perpendicular to the wall, then you can probably fix the problem by shiming behind a hinge Open the door all the way and then check it with a level. Add a shim behind where the hinge attaches to the door jamb to get it level. Again this is putting a band-aid on the real problem. To get this absolutely correct, you should remove the trim around the door jamb and re-hang the door. But a little bit of hinge moving might do the trick.
My explanation may be confusing to read. If you have any questions just respond with more questions.