There have been numerous attempts to explain the emergence of this expression, which seems to have swept into popular use in the US during the mid-19th century. Most of them are pure speculation. It does not seem at all likely, from the linguistic and historical evidence, that it comes from the Scots expression och aye, the Greek ola kala ('it is good'), the Choctaw Indian oke or okeh ('it is so'), the French aux Cayes ('from Cayes', a port in Haiti with a reputation for good rum) or au quai ('to the quay', as supposedly used by French-speaking dockers), or the initials of a railway freight agent called Obediah Kelly who is said to have written them on documents he had checked.
A more likely explanation is that the term originated as an abbreviation of orl korrekt , a jokey misspelling of 'all correct' which was current in the US in the 1830s. The oldest written references result from its use as a slogan by the Democratic party during the American Presidential election of 1840. Their candidate, President Martin Van Buren, was nicknamed 'Old Kinderhook' (after his birthplace in New York State), and his supporters formed the 'OK Club'. This undoubtedly helped to popularize the term (though it did not get President Van Buren re-elected).
The only other theory with at least a degree of plausibility is that the term originated among Black slaves of West African origin, and represents a word meaning 'all right, yes indeed' in various West African languages. Unfortunately, historical evidence enabling the origin of this expression to be finally and firmly established may be hard to unearth.
See other questions about the origins of phrases and words.
You may also be interested in: What is the origin of the word 'penguin'?
Or take a look at: Is a banana a fruit or herb?