Heinen scored his second goal to put the Bruins ahead 2-1 at 13:27 of the second.
After defenseman Brandon Carlo's slap shot deflected wide, the puck rebounded off the end wall to Heinen, who was waiting near the left post to stuff it in Sharks goaltender Martin Jones.
Khudobin made 15 saves in the third period, including five during a 6-on-4 with Carlo in the penalty box after being called for slashing at 17:28 and Jones on the bench for the extra attacker.
The win provided some redemption for the Bruins and Khudobin after they squandered a three-goal lead in a 5-4 overtime loss to the Buffalo Sabres on Saturday.
"[Khudobin's] always going to have a bit of ... energy, for lack of a better word, in there, and more movement than most. But I thought he was very composed and under control for his game," Bruins coach Bruce Cassidy said. "Puck play was solid. Very focused, seemed like he was taking those details in terms of positioning. Like I said, he's not your typical technically robotic goaltender. But I thought it was a very good bounce-back game for him."