If an attacking player accidentally touches the ball with their hand or arm and then scores a goal, or the ball goes to another attacking player and they immediately score, this is a handball offence.
But it is not a handball offence if after an accidental handball the ball travels some distance via a pass or a dribble, or there are several passes before the goal or goalscoring opportunity.