Descartes's original statement was "Je pense donc je suis," from his Discourse on Method (1637). He uses the Latin "Cogito ergo sum" in the later Principles of Philosophy (1644), Part 1, article 7: "Ac proinde hæc cognitio, ego cogito, ergo sum, est omnium prima & certissima, quæ cuilibet ordine philosophanti occurrat.", by which time it had become popularly known as 'the "Cogito Ergo Sum" argument'.
But if life itself is good and pleasant (...) and if one who sees is conscious that he sees, one who hears that he hears, one who walks that he walks and similarly for all the other human activities there is a faculty that is conscious of their exercise, so that whenever we perceive, we are conscious that we perceive, and whenever we think, we are conscious that we think, and to be conscious that we are perceiving or thinking is to be conscious that we exist... (Nicomachean Ethics, 1170a25 ff.)
St. Augustine of Hippo in De Civitate Dei writes "Si […] fallor, sum" ("If I am mistaken, I am") (book XI, 26), and also anticipates modern refutations of the concept. Another predecessor was Avicenna's "Floating Man" thought experiment on human self-awareness and self-consciousness. Nevertheless, Descartes was original in expanding the idea as a defence against the growing scepticism of his time and using it as a foundation of his whole metaphysics.
At the beginning of the second meditation, having reached what he considers to be the ultimate level of doubt — his argument from the existence of a deceiving god — Descartes examines his beliefs to see if any have survived the doubt. In his belief in his own existence he finds it is impossible to doubt that he exists. Even if there were a deceiving god (or an evil demon, the tool he uses to stop himself sliding back into ungrounded beliefs), his belief in his own existence would be secure, for how could he be deceived unless he existed in order to be deceived?
But I have convinced myself that there is absolutely nothing in the world, no sky, no earth, no minds, no bodies. Does it now follow that I too do not exist? No. If I convinced myself of something [or thought anything at all] then I certainly existed. But there is a deceiver of supreme power and cunning who is deliberately and constantly deceiving me. In that case I too undoubtedly exist, if he is deceiving me; and let him deceive me as much as he can, he will never bring it about that I am nothing so long as I think that I am something. So, after considering everything very thoroughly, I must finally conclude that the proposition, I am, I exist, is necessarily true whenever it is put forward by me or conceived in my mind. (AT VII 25; CSM II 16–17)
There are three important notes to keep in mind here. First, he only claims the certainty of his own existence from the first-person point of view — he has not proved the existence of other minds at this point. This is something that has to be thought through by each of us for ourselves, as we follow the course of the meditations. Second, he is not saying that his existence is necessary; he is saying that if he's thinking, then necessarily he exists (see the instantiation principle). Third, this proposition "I am, I exist" is held true not based on a deduction (as mentioned above) nor on empirical induction, but on the clarity and self-evidence of the proposition.
Descartes does not use this first certainty, the cogito, as a foundation upon which to build further knowledge; rather, it is the firm ground upon which he can stand as he works to restore his beliefs. As he puts it:
Archimedes used to demand just one firm and immovable point in order to shift the entire earth; so I too can hope for great things if I manage to find just one thing, however slight, that is certain and unshakable. (AT VII 24; CSM II 16)
According to many of Descartes' specialists, including Étienne Gilson, the goal of Descartes in establishing this first truth is to demonstrate the capacity of his criterion — the immediate clarity and distinctiveness of self-evident propositions — to establish true and justified propositions despite having adopted a method of generalized doubt. As a consequence of this demonstration, Descartes considers science and mathematics to be justified to the extent that their proposals are established on a similar immediate clarity, distinctiveness, and self-evidence that present itself to the mind. The originality of Descartes' thinking, therefore, is not so much in expressing the cogito — a feat accomplished by other predecessors, as we have seen — but on using the cogito as demonstrating the most fundamental epistemological principle, that science and mathematics are justified by relying on clarity, distinctiveness, and self-evidence.