The best thing to avoid guitar strings going out of tune is:
(a) Buy a quality instrument .
(b) When you put the strings in, give them plenty of "neatly organised" winds on the pegs.
(c) When you put new strings on, give them some big bends to begin with, especially the treble strings.
(c)...