I saw that at some schools of some martial arts many of the students wear headbands. They seem to only be in certain martial arts (like Karate), and I would almost never see it in others (like BJJ or Judo). Do these headbands have a historical or contextual meaning? If so, what do the different colors/patterns mean? Also, why are they worn in the first place? My first assumption was that they are there to soak up sweat, but then why aren't they worn in most martial arts?
Assuming you're talking about Japanese martial arts and Hachimaki, it's basically a matter of tradition that might be based off of samurai helmets and padding against cuts. These days, it's more ceremonial, and sometimes a method of advertising (hachimaki with slogans are passed out at trade shows), but they do still work to keep sweat out of your eyes.
The origin of hachimaki is uncertain. The most common theory states that they originated as headbands worn by samurai to line their heads with cloth. This was to stop cuts from the helmet and make wearing the helmet more comfortable.
In Muay Thai, the mongkoi is entirely ceremonial, and is basically a sign of rank, much like belts are in many styles.
I have not found any reliable material on why some Pencak Silat fighters have it as part of the their uniform other than tradition.