How Yoga and Samkhya school of Hindu philosophy are related? I know that Samkhya and Yoga schools are closely related. But I can not understand one thing. Samkhya is an atheistic school, whereas Yoga is theistic. How to reconcile that?
To give a first answer: Samkhya is the theory, while Yoga is the corresponding practice.
The goal of Samkhya is to end suffering. To obtain this goal „a superior method […] is the discriminative knowledge of the manifest, the unmanifest and the knowing one“ (Karika II). Samkhya elaborates the relevant knowledge as a worldview based on the concept of evolution.
While Yoga is a method of meditation according to its basic text, Patanjali’s Yoga Sutra: „Yoga is the stilling of the changing states of the mind“ (Sutra I,2).
On one hand, Samkhya does not refer to any god. Prakriti (= matter) has no beginning. Structure formation is transformation of matter by the catalytic force of purusha (= soul, consciousness).
On the other hand, according to Yoga it is possible to attain the desired state of consciousness „from devotion to the Lord (isvara)“ (Sutra I,23). Though, it is left open which personal god should take the place of the Lord.
According to the comments of Larson and Bhattacharya ("Encyclopedia of Indian Philosophies. Vol. XII. Yoga: India's Philosophy of Meditation") the Lord of the Yoga Sutras is one specific purusha from the many purushas of Samkhya. When reading their explanations (p.91ff), the "Lord" seems to be a rather abstract and vague concept, without sharp characteristics. In particular, the Lord has no personality.
There is a discussion between Yudhishthira and Bhishma on this issue.
Yudhishthira siad, 'It behoveth thee to explain to me, O sire, what the difference is between the Sankhya and the Yoga system of philosophy. O foremost one of Kuru's race, everything is known to thee, O thou that art conversant with all duties!'
Bhishma said, 'The followers of Sankhya praise the Sankhya system and those regenerate persons that are Yogins praise the Yoga system. For establishing the superiority of their respective systems, each calls his own system to be the better. Men of wisdom devoted to Yoga assign proper and very good reasons, O crusher of foes, for showing that one that does not believe in the existence of God cannot attain to Emancipation. Those regenerate persons, again, that are believers in the Sankhya doctrine advance good reasons for showing that one by acquiring true knowledge of all ends, becomes dissociated from all worldly objects, and after departing from this body, it is plain, becomes emancipated, and that it cannot be otherwise. Men of great wisdom have thus expounded the Sankhya doctrine of Emancipation. When reasons are balanced on both sides, those that are assigned on that side which one is otherwise inclined to adopt as one's own, should be accepted. Indeed, those words that are said on that side whould be regarded as beneficial. Good men may be found on both sides. Persons like these may adopt either opinion. The evidences of Yoga are addressed to the direct ken of the senses; those of Sankhya are based on the scriptures. Both systems of philosophy are approved by me, O Yudhishthira. Both these systems of science, O king, have my concurrence and are concurred in by those that are good and wise. If practised duly according to the instructions laid down, both would, O king, cause a person to attain to the highest end.In both systems purity is equally recommended as also compassion towards all creatures, O sinless one. In both again, the observances of vows has been equally laid down. Only the scriptures that point out their paths are different.
Mahabharata Santi Parva Section CCCI
How does a Yogi attain emancipation?
Bhishma said, 'By casting off, through the aid of Yoga, these five faults, viz., attachment, heedlessness, affection, lust and wrath, one attains to emancipation.
Mahabharata Santi Parva Section CCCI
How does a follower of Sankhya attain emancipation?
Bhishma said, '..beholding the birth, decrepitude, death, and sorrows of creatures, knowing truly the faults attaching to the body and the sorrows to which human beings are subject, and the vicissitudes to which the bodies of creatures are subject, and understanding all the faults that attach to their own souls, and also the inauspicious faults that attach to their own bodies (the followers of the Sankhya philosophy succeed in attaining to Emancipation).
Mahabharata Santi Parva Section CCCII
Bhishma seems to be saying that both Yoga and Sankhya enable a follower to get rid of faults and attain emancipation. The difference between the two is that an Yogi takes help from God while a Sankhya follower (now called Jnana Yogi) tries to get rid of faults by knowing about the various faults.
I am adding this section to better explain the Mahabharata quote. Both Sankhya and Yoga accept the Sankhya theory of Purusha and Prakriti. The aim of both is to go beyond Prakriti. This requires purity of the body-mind complex and compassion mentioned in the bolded part of the Mahabharata quote.
The Mahabharata quote also talks of difference in scripture. What is this difference?
From Special Devotion to Isvara also (concentration becomes imminent).
Patanjali Yoga Sutra I.23
Who is this Isvara?
Isvara is a particular Purusha unaffected by affliction, deed, result of action or the latent impression thereof.
Patanjali Yoga Sutra I.24
The Yoga system talks of a special purusha called Isvara whose help is recommended to go beyond Prakriti. The Sankhya system does not have any such concept.
To sum up the 2 systems of Sankhya and Yoga are related because they share the same metaphysics (Purusha and Prakriti), the same moral requirement (purity and compassion) and the same goal (freedom from Prakriti). Their difference is due to the Yoga scripture disagreeing with the Sankhya theory by postulating God or Isvara as a particular purusha which is not there in the Sankhya scriptures.