OS is responsible for choosing which threads run on the processor.
To be more specific, the scheduler is responsible for choosing what threads execute on the processor, which is an operating system module that selects the next jobs to be admitted into the system and the next process to run.
FYI, https://en.wikipedia.org/wiki/Scheduling_(computing) also introduces schedule disciplines such as FIFO, EDF, etc.
When the OS schedules the tasks, is there certain priorities with foreground tasks and background tasks?
That is up to the operating system to decide. If I remember correctly, Windows chooses to assign non-foreground processes lower priority since latency according to the user is usually less important. Linux has two different priority schemes. For non-realtime tasks it uses "niceness" values from -20 to 19 where lower values are "less nice" and therefore higher priority. Realtime tasks have priorities from 1 to 99 where 1 is the highest priority and 99 is the lowest. I believe all tasks marked as realtime run at higher priority than non-realtime tasks.
With these many threads, only 8 threads are active concurrently (4 cores, 2 execution contexts each), and only 4 can run simultaneously (each core runs 1 instruction per clock).