I wanted to do one post, but it has gotten to long so I'll first write a post about the mechanics of combat and then later a second one explaining how to use that knowledge.
Combat is divided into three phases: skirmish, melee, and pursue. Unit types have given morale, attack, and defense values. There are separate attack and defense values for each phase of combat. You can check the wiki link I posted for a chart with all the stats. The various unit types can be broadly divided into skirmish oriented (Light Infantry, Archers, Light Cavalry, Horse Archers, Camel Cavalry) and melee oriented (Heavy Infantry, Heavy Cavalry, Pikemen, War Elephants) based on which phase of combat they do best in. Skirmish oriented unit types also do well at pursuing fleeing enemies.
Units are distributes among 3 flanks for each of the two sides, creating a total of 6 flanks. At any given time each of these flanks will be using one of the many available combat tactics. You can get all the details about the many tactics on the wiki:
https://ck2.paradoxwikis.com/Combat_tactics
Every tactic in the game has conditions which must be met in order for a flank to be able to choose it (this is often called triggering a tactic) and additional "weight modifiers" that make it more likely to trigger. The most important condition is the composition of the flank (how many soldiers of each unit type there are in the flank). Every single tactic requires a certain composition, such as minimum and maximum proportions of specific unit types. Some also require that the battle takes place on specific terrain or the commander to have a certain Martial ability, a specific culture, or specific traits. Not all tactics are good either. Some are designed to be harmful and even the good ones are often a double-edged sword in that they make one type of unit stronger and others weaker.
The Harass tactic for example will make any Light Cavalry in the flank fight much better, but it will also make all the Archers and Horse Archers completely useless. This means that ideally you'd want your entire army be made out of that one type of unit so that all your soldiers can get the bonus and none of them gets the malus. In practice however this isn't easy to achieve.
Each tactic is also part of a tactics group and many will multiply damage done against any enemy flank using tactics from a certain group. For example there is a Charge group and all the individual charge tactics (Charge, Charge on Undefended, Powerful Charge, Heroic Counter-charge, ...) belong to it. And if a flank is for example using the Force Back tactic it will do 4x damage if the enemy flank it's fighting is using any tactic from the Charge group.
There are 7 groups of tactics: Harass, Volley, Swarm, Defensive, Charge, Advance, and Stand Fast. They form a sort of paper-rock-scissors system, or rather two such systems (one for skirmish and one for melee). The skirmish set is Harass beats Swarm beats Volley beats Harass and the tactics deal 2x damage against the tactic group they beat. In melee Charge beats Advance beats Stand Fast beats Charge and the tactics deal 4x damage against the tactics group they beat. Defensive tactics neither counter nor get countered by any enemy tactics. There are also several tactics that break these rules, but they aren't that common. For example Berserker Charge is a Charge tactic that deals 2x damage against Stand-Fast tactics. These multipliers are called Affinity.
Battles start in skirmish phase and all the flanks will use skirmish tactics. Eventually they'll start triggering special Charge tactics which are skirmish tactics but change the phase of combat to melee. Once two engaged flanks are in melee they will start triggering melee tactics. It's normally impossible to go back to skirmish phase once you've entered melee phase. The only exception is if you have a commander who has a culture from the Altaic group (the various steppe peoples) and a cavalry army. Then you can get a special tactic that allows moving back to skirmish phase.
Every day of battle the attack values of all the soldiers in a flank are modified by whatever bonuses and penalties apply (the ones from tactics usually are the most important) and then added together and multiplied with ATTACK_TO_DAMAGE_MULT, which is game setting that's set to 0.01 in the unmodded game. That's the flank's damage. Or to put it a bit differently, damage is equal to 1% of all the attack values of the soldiers in the flank put together. This damage then gets multiplied by the Affinity multiplier if one applies and that's then the final damage.
This damage is then divided equally among all the soldiers on the opposing flank. That's the "damage per man". How many soldiers of each type get killed is determined by multiplying damage per man with the number of soldiers of that type and then dividing that value with the defense rating of the unit type for whichever phase of battle it's in. For example let's say you're in skirmish phase and have 100 Light Infantry and 100 Pikemen in a flank and that flank takes 40 damage. There are 200 soldiers in the flanks so the damage per man is 40/200 = 0.2. Light Infantry has a skirmish defense value of 2 so you lose 0.2*100/2= 10 Light Infantry. And the skirmish defense value of Pikemen is 4 so you lose 0.2*100/4=5 Pikemen.
Morale damage is complicated and there is a strange effect that I haven't gotten to the bottom of yet. But it is enough to know that if you do twice as much damage you will also do twice as much morale damage. The morale of a flank is the total morale value of all surviving soldiers minus any morale damage the flank has suffered. Any soldiers that have died are no longer contributing their morale score to the flank, so what the game considers 100% morale will go down throughout the course of battle. Once morale drops too low a flank will be routed and will enter the pursuit phase.
Once a flank beats it's opposing flank and sends it running it will either pursue it or it can attack another enemy flank. If a flank is attacking a flank that is already busy fighting a different flank that's called flanking and it results in bonus damage.
And I believe that's more or less it for the raw mechanics of combat. There are some details I haven't covered, but the post has gotten long enough even without them.