First things first, truly speaking there's no karma and rebirth in reality.
In true Sanatan Vedic Dharma, there's no concept of God who keeps track of the karma of Jiva and punishes them according to those.
Then from where do karma and rebirth come? I will discuss that later on. But before that let me establish why there can't be any karma or rebirth.
सदृशं चेष्टते स्वस्याः प्रकृतेर्ज्ञानवानपि। प्रकृतिं यान्ति भूतानि
निग्रहः किं करिष्यति।।3.33।।
3.33 Even a man of highest knowledge (jnani) acts in accordance with his own nature (prakriti); all beings follow their own Nature; what
can restraint do?
Now you see, whether you are an ordinary person following him/her whims or a person of perfect knowledge, you cannot act differently from your prakriti.
Abhinavagupta makes it more clear in his commentary.
।।3.33।।सदृशमिति। योऽपि च ज्ञानी न तस्य व्यवहारे भोजनादौ विपर्यासः
कश्चित्। अपि तु सोऽपि सत्त्वाद्युचितमेव चेष्टते एवमेव जानन्। यतः ( N
अतः) भूतानां पृथिव्यादीनां प्रकृतौ विलयः आत्मा च अकर्ता नित्यमुक्त इति
3.33 Sadrsam etc. There is hardly any difference in the worldly activities like eating etc., of him who is a man of wisdom. But he too
acts only in conformity to the Sattva, etc., Therefore knowing that:
'Because the elements like the Earth, etc. get absorbed into the
prakrti; and the Atma is also a non-doer and ever-freed; therefore the
eradication of birth etc., is for whose sake?' Then how can there be
bondage at all? That is as follows, it is said :
Here Abhinavagupta points out two interesting facts here. One, that when a person dies, all the elements go back to prakriti and Atma is forever free, so how can there be things like rebirth which need to be get rid off. And the other more interesting question which arises now is that if Atma is non-doer and Jivas only follow their inner instinct (prakriti), why should anyone be in bondage? Because looking at all this, there shouldn't be any bondage in nature but still we experience bondage. Now, that's a million-dollar question.
In Brihadaranyak Upanishad, this question is asked - If elements of body merge back to prakriti and Atma is forever free, why is Jiva reborn? In Chandogya Upanishad, shvetakatu asks a similar question that when manas (mind) merged back to Brahman in deep sleep, how can the Jiva be born again the next day. The answer to both these questions is gives as - karma.
It is because of this karma that Jiva are reborn. But what is this karma? Let me tell you its not how its told today. These days its told that its like an account of good and bad acts which you reap. This is an oversimplification and doesn't help anyone. The truth is more than this.
Let's now see what this karma truly is, from Niralamba Upanishad. And here lies key to your question -
कर्मेति च क्रियमाणेन्द्रियैः कर्माण्यहं करोमीत्यध्यात्मनिष्ठतया कृतं
कर्मैव कर्म कर्तृत्वभोत्त्कृत्वाद्यहंकारतया बन्धरूपं जन्मादिकारणम्॥
अकर्मेति नित्यनैमित्तिकयागव्रततपोदानादिषु फलाभिसन्धानं यत्तदकर्म॥
The actions are performed by indriya (sensory organs), when one starts
believing that I am the doer, only that action counts as karma. And
this karma done with the feeling of being karta (doer) and bhokta
(enjoyer) is the cause of bondage and rebirth. The actions which are
done on a daily basis as they fall upon oneself, (even) for attaining a
fruit doesn't count as karma. (because one knows they are not doer and doing it just because they have a body)
Read the above again and again and again. That's very very very important point.
The same idea is reflected in nyaya sutra 1.1.2 -
दुःखजन्मप्रवृत्तिदोषमिथ्याज्ञानानाम् उत्तरोत्तरापाये तदनन्तरा पायात्
According to nyaya sutra, mitha jnana (false knowledge) leads to dosha (that which can cause problem), dosha further leads to pravriti (the impulsion to do something/karma), pravriti further leads to birth and birth leads to suffering.
As you can see in Vedanta as well as Nyaya Sutra the fundamental principle is that suffering of Jiva is a result of ignorance only. On the surface level, Sanatan Dharma says that suffering is a result of bad karma but truly speaking, all sufferings are a result of ignorance.
Let me give more evidance because no one believes me when I tell them this -
अहं ब्रह्मेति विज्ञानात्कल्पकोटिशतार्जितम् । सञ्चितं विलयं याति
प्रबोधात्स्वप्नकर्मवत् ॥ ४४७ ॥
- Through the realisation of one’s identity with Brahman, all the accumulated actions of a hundred crore of cycles come to nought, like
the actions of dream-state on awakening.
यत्कृतं स्वप्नवेलायां पुण्यं वा पापमुल्बणम् । सुप्तोत्थितस्य किं
तत्स्यात्स्वर्गाय नरकाय वा ॥ ४४८ ॥
- Can the good actions or dreadful sins that a man fancies himself doing in the dream-state, lead him to heaven or hell after he has
awakened from sleep?
The above is from Vivekachudamani. This answer has gone too big and I'm not even sure whether anyone appreciate what I'm writing or not. So, I will stop here. If anyone is interested I can explain the 2 points which are still left to explained - first, how can illusion of rebirth and karma arise when its not present in reality. Second what causes the bondage in a living being when all their work are indeed a result of prakriti, and why there's so much importance given to doing the right action. And another side question is what is the right action which leads to liberation aka स्वधर्म.
Let me answer the remaning questions below.
Question: What causes bondage in a living being when all their work are indeed a result of prakriti?
Answer: The bondage in a living being is caused by राग (affection) and द्वेष (repulsion) towards objects. The likes and dislikes for objects should belong to the sense organs but because of ignorance, a living being starts thinking that it belong to His Own Atma. This is the bondage for a living being, nothing else. When such a person doesn't get what they want to get, or they get what they dislike, it causes suffering because they have assumed they are suffering instead of understanding that its their sense organs only which are suffering not them. It's the same as a person hooked to a dream or video game. In reality he doesn't suffer but he thinks he suffers. If one can get rid of superimposing likes and dislikes in their Atma, then one is competely free. By default people are like that only as you can see a child. In Yoga also, a perfect Yogi is one who acts like a transparent gem (स्फटिक मणि) ie he doesn't take the impressions of the world to his heart. Therefore the bondage of living beings is not something unseen or some sort of karma balance but it is one's ignorance that forces them to superimpose affection towards objects of desire onto their own Atma. That's why Shri Krishna says
इन्द्रियस्येन्द्रियस्यार्थे रागद्वेषौ व्यवस्थितौ। तयोर्न वशमागच्छेत्तौ
3.34. [For a man of worldly life] there are affection and repulsion clearly fixed with regard to the objects of each of his sense organs.
These are the obstacles for him. [The wise] would not come under the
control of these.
Shri Abhinavagupta commentary -
कथं तर्हि बन्धः इत्थमित्युच्यते (N omits इत्थम् K omits
इति)।इन्द्रियस्येति। श्रेयानिति। संसारी च प्रतिविषयं रागं द्वेषं च
गृह्णाति यतः कर्माणि आत्मकर्तृकाण्येव विमूढत्वादभिमन्यते इति सममपि
भोजनादिव्यवहारं कुर्वतोः ज्ञानिसंसारिणोरस्त्ययं विशेषः।
A person living the worldly life does entertain affection or repulsion
towards every sense-object. For, due to his ignorance he
imagines that actions are performed only by him. Thus there is
this difference between a man of knowledge and a man of worldly life,
eventhough they perform alike their [respective] worldly activities
such as eating etc.
Second question: How can illusion of rebirth and karma arise when its not present in reality?
Answer: The bodies of living beings are continously arising and merging back in the flux of prakriti, just like waves arise and subside in the ocean. When a being dies with a certain sort of samskara / vritti (conditioning or tendency) and when the same sort of samskara (conditioning or tendency) is seen in another being we call it rebirth. This is rebirth nothing else. As long as the vritti or samskar keep arising we say Jiva is taking birth. Jiva is also nothing but certain tendencies or vasanas. This tendency is hard to defeat. The only way to defeat it is by first accepting that it exists (although it doesn't). Then we kill it by using its force against itself. That is we kill the karma by first accepting karma is there and then doing satvic karmas to get rid of all karma. Similarly although Atma is free from vritti, satva vritti has to be first developed inorder to kill all other vritti. That's why doing the right karma is very important. And as long as one is not at a certain level a traditional Guru never encloses the knowledge of तत्त्वमसि (You are Brahman) his shishya (disciple). A traditional Guru you will find will keep insisting on the reality of karma and rebirth.
In the end a very interesting conversation between Yajnavalkya and Artabhag -
याज्ञवल्क्येति होवाच, यत्रास्य पुरुषस्य मृतस्याग्निं वागप्येति, वातं
प्राणः, चक्शुरादित्यम्, मनश्चन्द्रम्, दिशः श्रोत्रम्, पृथिवीं शरीरम्,
आकाशमात्म, ओषधीर्लोमानि, वनस्पतीन्केशाः, अप्सु लोहितं च रेतश्च
निधीयते, क्वायं तदा पुरुषो भवतीति; अहर सोम्य हस्तमार्तभा, आवामेवैतस्य
वेदिष्यावः, न नावेतत् सजन इति । तौ होत्क्रम्य मन्त्रयांचक्राते; तौ ह
यदूचतुः कर्म हैव तदूचतुः, अथ यत्प्रशशंसतुः कर्म हैव तत् प्रशशंसतुः;
पुण्यो वै पुण्येन कर्मणा भवति, पापः पापेनेति । ततो ह जारत्कारव आर्तभाग
उपरराम ॥ १३ ॥ इति द्वितीयं ब्राह्मणम् ॥
- ‘Yājñavalkya,’ said he, ‘when the vocal organ of a man who dies is merged in fire, the nose in air, the eye in the sun, the mind
in the moon, the ear in the quarters, the body in the earth, the ether
of the heart in the external ether, the hair on the body in the herbs,
that on the head in the trees, and the blood and the seed are
deposited in water, where is then the man?’ ‘Give me your hand, dear
Ārtabhāga, we will decide this between ourselves, we cannot do it in
a crowd.’ They went out and talked it over. What they mentioned
there was only karma, and what they praised there was also only karma.
(Therefore) one indeed becomes good through good work and evil through
evil work. Thereupon Ārtabhāga, of the line of Jaratkāru, kept