teilnehmen an
to participate in
To be present at and take part in an event, course, competition, or activity.
Nächstes Jahr möchte ich am Stadtmarathon teilnehmen.
Next year I would like to participate in the city marathon.

to participate / sympathize
Loading your saved state.
to participate in
To be present at and take part in an event, course, competition, or activity.
Nächstes Jahr möchte ich am Stadtmarathon teilnehmen.
Next year I would like to participate in the city marathon.

'Teilnehmen' (to participate / sympathize) is a separable verb. In a main clause, the prefix 'teil' stands at the end of the sentence. In subordinate clauses and infinitive constructions, the verb stays together.
Er nimmt regelmäßig an den Treffen teil.
He regularly participates in the meetings.
'Teilnehmen' (to participate / sympathize) is always used with the preposition 'an' and the dative. You take part 'an etwas' (in something) or 'an jemandem' (with someone/in someone's situation).
Sie hat an einem interessanten Workshop teilgenommen.
She participated in an interesting workshop.
The verb is made up of 'Teil' (part) and 'nehmen' (to take). Literally, it means 'to take a part of something,' which explains the idea of participation well.
Über hundert Läufer nehmen am Marathon teil.
Over a hundred runners are participating in the marathon.
The meaning 'Mitgefühl zeigen' (to show sympathy) is formal and is used almost exclusively in the context of grief and condolences, often in written form.
Wir nehmen aufrichtig an Ihrem schweren Verlust teil.
We sincerely sympathize with you in your heavy loss.
there / with you
'dabei' and 'teilnehmen' can both mean that someone is at an event or activity. 'dabei' is more general and often more colloquial: someone is there with the others or joins in. 'teilnehmen' sounds more precise and a little more official. It fits especially with courses, exams, competitions, or events where you participate. The confusion arises because 'dabei sein' (to be there / to be involved) can sometimes seem almost like 'teilnehmen'. But 'teilnehmen' emphasizes active or official participation more strongly.

Ich nehme an einem Kurs teil, um mich auf die Prüfung vorzubereiten.
I am taking part in a course to prepare for the exam.
Nach der Nachricht war die Fahrerin beim Treffen doch dabei.
After the message, the driver was there at the meeting after all.