I just want to ask if it is okay to say to your boss who is going home after work お疲れ様でした気をつけてください or it is impolite?
Yes and no.
The phrase 気を付けてください alone is in polite form (towards a person of a higher standing slightly better would be: お気を付けてください). However it does feel a little bit more like "please take care" rather than casual English greeting "take care". It would be strange if you used the phrase every single time a person leaves.
To strangers or people with whom you rarely meet, or towards a person you meet every day, but in an unusual situation like a typhoon coming, a phrase お気を付けてお帰りください would be very appropriate.
Then again, you might contract it to the previous お気を付けてください, or お気を付けて depending on familiarity, context, characters left in an electronic message, etc. I guess your teacher considered your situation of being late to be unusual.
(お気を付けてください would be also appropriate for a variety of situations, like towards someone going for a business trip, going abroad for holidays, or when you want to be nice and show your gratitude for something (being invited to a dinner), but you asked specifically for "going home after work" and that might feel a bit strange repeated day by day)
(...yet again, if you make it a ritual to say so, you're good to go, then a shorter お気をつけて might work best, I guess)