Since the Related to (What) field on the Task object is a polymorphic field, first you need to check what kind of record the task record has been created for. and then you can assign the Campaign field on a before trigger based on your requirements, just like this:
trigger TaskTrigger on Task (before insert) {
for (Task t : Trigger.new) {
// replace 'Sales' with your Object API Name
if (t.WhatId.getSobjectType().getDescribe().getName() == 'Sales') {
// update below field with your Campaign API Field name
t.Campaign__c = t.WhatId;
}
}
}
useful links to look at:
Hope this helps with your question.