Is the stabbed enemy affected by the spell?
Yes*. Heat Metal states:
Any creature in physical contact with the object takes 2d8 fire damage when you cast the spell. Until the spell ends, you can use a bonus action on each of your subsequent turns to cause this damage again.
Having a knife in your back certainly counts as physical contact.
Does he take damage from the spell until he pulls it out?
Again, yes*, as long as the caster uses a bonus action on their turn.
*However, there are no rules for leaving a knife in someone. That would be up to the DM to allow or deny.
Heat Metal also states
If a creature is holding or wearing the object and takes the damage from it, the creature must succeed on a Constitution saving throw or drop the object if it can. If it doesn't drop the object, it has disadvantage on attack rolls and ability checks until the start of your next turn
I don't think having a knife embedded in you would count as 'holding' or 'wearing' the knife, but that could be up to DM interpretation.