The net gain of ATP in glycolysis is 2 ATP. Even though the reaction of glycolysis initially uses 2 ATP in its overall process, 4 ATP are produced toward the end.
The purpose of glycolysis is the production of energy inside cells. Glycolysis is just one of the methods used by cells in this regard, with many products - such as molecules of pyruvic acid - entering more complex cycles, such as Kreb's cycle, in the production of yet more ATP. This ATP is a necessary part of the diet of cells, assisting them in avoiding the build up of toxic waste products.